-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathFindAllDuplicates.java
More file actions
50 lines (45 loc) · 1.08 KB
/
FindAllDuplicates.java
File metadata and controls
50 lines (45 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.java.array;
import java.util.HashSet;
/*
* Find All duplicate elements
* ---------------------------
* Write java program to find all the duplicates
* elements from the given array.
*
* This solution will work only when array contains elements
* value lesser than array size.
*
* Suppose the array size is 5, and array contains value 10
* For this case below code will not work.
*
*/
public class FindAllDuplicates {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
// int array[] = {4 , 2, 4, 5, 2, 3, 1, 2, 3};
// int array[] = {2, 1, 2, 2, 1, 3};
int arr[] = {3, 8, 1, 3, 6, 3, 8, 1, 6};
for(int i =0;i<arr.length;i++){
if(arr[Math.abs(arr[i])] > 0)
arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])];
else{
set.add(Math.abs(arr[i]));
}
}
System.out.println("All duplicates are :: "+set);
}
}
/*
INPUT
{4,2,4,5,2,3,1,2,3}
OUTPUT
All duplicates are :: [2, 3, 4]
INPUT
{2, 1, 2, 2, 1, 3}
OUTPUT
All duplicates are :: [1, 2]
INPUT
{3, 8, 1, 3, 6, 3, 8, 1, 6};
OUTPUT
All duplicates are :: [1, 3, 6, 8]
*/