Skip to content

Commit 4b0e0a7

Browse files
committed
finds duplicate elements in array in O(N^2) time
1 parent 4480496 commit 4b0e0a7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Leetcode_August_Challenge;
2+
3+
import java.util.ArrayList;
4+
5+
public class arrayDuplicates {
6+
7+
public static void main(String[] args) {
8+
9+
int[] arr = { 4, 3, 2, 7, 8, 2, 3, 1, 3};
10+
11+
System.out.println(findDuplicates(arr));
12+
}
13+
14+
public static ArrayList<Integer> findDuplicates(int[] arr) {
15+
ArrayList<Integer> result = new ArrayList<Integer>();
16+
17+
for (int cursor = 0; cursor < arr.length - 1; cursor++) {
18+
for (int rest = cursor + 1; rest < arr.length - 1; rest++) {
19+
if (arr[cursor] == arr[rest]) {
20+
result.add(arr[cursor]);
21+
}
22+
}
23+
}
24+
return result;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)