Skip to content

Commit 9adfbdf

Browse files
authored
Create 442_Find_All_Duplicates_in_an_Array.java (qiyuangong#81)
Contributed by @ajay2614
1 parent 5a52cd9 commit 9adfbdf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<Integer> findDuplicates(int[] nums) {
3+
int n = nums.length;
4+
5+
List<Integer> ans = new ArrayList<>();
6+
7+
for(int i=0;i<n;i++) {
8+
int possibleVal = Math.abs(nums[i]);
9+
10+
if(nums[possibleVal - 1] < 0) {
11+
ans.add(possibleVal);
12+
}
13+
nums[possibleVal - 1] = -1 * nums[possibleVal - 1];
14+
}
15+
return ans;
16+
}
17+
}

0 commit comments

Comments
 (0)