-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuple.java
More file actions
28 lines (22 loc) · 911 Bytes
/
Tuple.java
File metadata and controls
28 lines (22 loc) · 911 Bytes
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
import java.util.*;
class Solution {
public int[] solution(String s) {
s = s.substring(1,s.length()-1).replace("{","").replace(","," ");
String str[] = s.split("}");
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<str.length; i++) {
StringTokenizer st = new StringTokenizer(str[i]);
while(st.hasMoreTokens()) {
int key = Integer.parseInt(st.nextToken());
map.put(key, map.getOrDefault(key, 0) + 1);
}
}
List<Map.Entry<Integer, Integer>> entryList = new LinkedList<>(map.entrySet());
entryList.sort((o1, o2) -> o2.getValue() - o1.getValue());
ArrayList<Integer> answer = new ArrayList<>();
for(var entry : entryList){
answer.add(entry.getKey());
}
return answer.stream().mapToInt(Integer::intValue).toArray();
}
}