|
@@ -0,0 +1,18 @@
|
|
|
|
|
+func findDuplicates(nums []int) (res []int) {
|
|
|
|
|
+ n := len(nums)
|
|
|
|
|
+ for i := 0; i < n; i++ {
|
|
|
|
|
+ if idx := abs(nums[i]) - 1; nums[idx] < 0 {
|
|
|
|
|
+ res = append(res, idx+1) // Twice
|
|
|
|
|
+ } else {
|
|
|
|
|
+ nums[idx] = -nums[idx] // Once
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return res
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func abs(x int) int {
|
|
|
|
|
+ if x < 0 {
|
|
|
|
|
+ return -x
|
|
|
|
|
+ }
|
|
|
|
|
+ return x
|
|
|
|
|
+}
|