-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDuplicateInArray.java
More file actions
48 lines (34 loc) · 1.13 KB
/
FindDuplicateInArray.java
File metadata and controls
48 lines (34 loc) · 1.13 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
package Testing;
import java.util.*;
public class FindDuplicateInArray {
public static void main(String[] args) {
String[] names = {"ddf", "Java", "Python", "C", "Ruby", "Java", "Python", "Perl"};
//using hashset
Set<String> store = new HashSet<String>();
for(String name : names)
{
if(store.add(name) == false)
{
System.out.println("Duplicate in array are:" +name);
}
}
// using hashmap
Map<String,Integer> storeMap = new HashMap<String, Integer>();
for(String name : names)
{
Integer count = storeMap.get(name);
if(count == null)
storeMap.put(name,1);
else
storeMap.put(name, ++count);
}
Set<Map.Entry<String,Integer>> entrySet = storeMap.entrySet();
for (Map.Entry<String,Integer> entry : entrySet)
{
if(entry.getValue() > 1)
{
System.out.println("duplicate elements are:" +entry.getKey()+ "Count is:" +entry.getValue());
}
}
}
}