-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDuplicateElementsPrintInArray.java
36 lines (31 loc) · 1.09 KB
/
DuplicateElementsPrintInArray.java
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
/*
1. Create and initialise input array.
2. Create an empty set for storing non duplcate elements
3. Create an empty set and name it as duplicate set.
4. Iterate through each element in array and check whether ND contains teh elements. If it is prsesent add it to dplicate set.
5. If it is not present add it to non duplicate set.
6. Finally print elements in duplicate set.
*/
package interviewprograms;
import java.util.HashSet;
import java.util.Set;
public class DuplicateElementsPrintInArray {
public static void main(String[] args) {
String findDuplicate[] = new String[]{"java", "c", "c++", "Python", "java", "python"};
Set nonDuplicateSet= new HashSet<>();
Set duplicateSet= new HashSet<>();
for (String string: findDuplicate) {
if(!nonDuplicateSet.contains(string)){
nonDuplicateSet.add(string);
}
else{
duplicateSet.add(string);
}
}
System.out.println(duplicateSet);
}
}
/**
run:
[java]
*/