Skip to content

Commit 0f030e7

Browse files
author
shrinathjoshi
committed
Updated solution to day 1 of Daily coding Challenge
1 parent de361f6 commit 0f030e7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* @author Shrinath Joshi
6+
*
7+
* Problem Statement :-
8+
* https://leetcode.com/problems/find-all-duplicates-in-an-array/
9+
*
10+
* Time complexity :- O(n)
11+
*
12+
* Space complexity :- O(1)
13+
*
14+
*/
15+
16+
public class FindAllDuplicatesInAnArray {
17+
public List<Integer> findDuplicates(int[] nums) {
18+
List<Integer> result = new ArrayList<Integer>();
19+
20+
for (int num : nums) {
21+
int number = Math.abs(num);
22+
if (nums[number - 1] < 0)
23+
result.add(number);
24+
else
25+
nums[number - 1] = -nums[number - 1];
26+
}
27+
return result;
28+
}
29+
30+
public static void main(String[] args) {
31+
int nums[] = { 4, 3, 2, 7, 8, 2, 3, 1 };
32+
33+
List<Integer> result = new FindAllDuplicatesInAnArray().findDuplicates(nums);
34+
35+
for (Integer num : result)
36+
System.out.println(num + " ");
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)