-
Notifications
You must be signed in to change notification settings - Fork 0
/
DuplicateCount.java
48 lines (42 loc) · 1.15 KB
/
DuplicateCount.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
37
38
39
40
41
42
43
44
45
46
47
48
package org.wellsfargo.com.wschat.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONObject;
public class DuplicateCount {
public static void main(String[] args) {
String hello = "hellp";
JSONObject jObject = new JSONObject(hello);
System.out.println(jObject);
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(1);
list.add(1);
list.add(2);
list.add(3);
list.add(2);
list.add(3);
list.add(5);
Set<Integer> uniqueSet = new HashSet<Integer>(list);
for (Integer temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list, temp));
}
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
for (Integer temp : list) {
Integer count = mp.get(temp);
mp.put(temp, (count == null) ? 1 : count + 1);
}
printMap(mp);
}
static void printMap(Map<Integer, Integer> mp) {
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
}
}
}