Find All Duplicates in an Array LeetCode solution in Java

1
class Solution {
2
public List<Integer> findDuplicates(int[] nums) {
3
List<Integer> ans = new ArrayList<>();
4
for (int i = 0; i < nums.length; i++) {
5
int ind = Math.abs(nums[i]) - 1;
6
if (nums[ind] < 0) {
7
ans.add(Math.abs(nums[i]));
8
} else {
9
nums[ind] = -1 * nums[ind];
10
}
11
}
12
return ans;
13
}
14
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0