Skip to content

Commit

Permalink
Create 442_Find_All_Duplicates_in_an_Array.java (#81)
Browse files Browse the repository at this point in the history
Contributed by @ajay2614
  • Loading branch information
ajay2614 authored May 19, 2023
1 parent 5a52cd9 commit 9adfbdf
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/442_Find_All_Duplicates_in_an_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public List<Integer> findDuplicates(int[] nums) {
int n = nums.length;

List<Integer> ans = new ArrayList<>();

for(int i=0;i<n;i++) {
int possibleVal = Math.abs(nums[i]);

if(nums[possibleVal - 1] < 0) {
ans.add(possibleVal);
}
nums[possibleVal - 1] = -1 * nums[possibleVal - 1];
}
return ans;
}
}

0 comments on commit 9adfbdf

Please sign in to comment.