SHOW:
|
|
- or go back to the newest paste.
1 | import java.util.ArrayList; | |
2 | import java.util.Collections; | |
3 | import java.util.Comparator; | |
4 | import java.util.HashMap; | |
5 | import java.util.HashSet; | |
6 | - | class MyClass |
6 | + | import java.util.Set; |
7 | - | { |
7 | + | |
8 | - | static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>(); |
8 | + | class MyClass { |
9 | - | public static void main(String[] arrg) |
9 | + | static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>(); |
10 | - | { |
10 | + | |
11 | - | int[] arr = {2,5,2,8,5,6,8,8}; |
11 | + | public static void main(final String[] arrg) { |
12 | - | map = new HashMap<Integer,Integer>(); |
12 | + | final int[] arr = { 2, 5, 2, 8, 5, 6, 8, 8 }; |
13 | - | ArrayList<Integer> list = new ArrayList<Integer>(); |
13 | + | map = new HashMap<Integer, Integer>(); |
14 | - | for(int i=0; i<arr.length; i++) |
14 | + | final Set<Integer> set = new HashSet<>(); |
15 | - | { |
15 | + | for (int i = 0; i < arr.length; i++) { |
16 | - | if(map.get(arr[i]) == null) |
16 | + | if (map.get(arr[i]) == null) { |
17 | - | { |
17 | + | map.put(arr[i], 1); |
18 | - | map.put(arr[i],1); |
18 | + | } else { |
19 | - | } |
19 | + | int count = map.get(arr[i]); |
20 | - | else |
20 | + | map.put(arr[i], ++count); |
21 | - | { |
21 | + | } |
22 | - | int count = map.get(arr[i]); |
22 | + | |
23 | - | map.put(arr[i],++count); |
23 | + | set.add(arr[i]); |
24 | - | } |
24 | + | |
25 | - | |
25 | + | final ArrayList<Integer> list = new ArrayList<Integer>(set); |
26 | - | list.add(arr[i]); |
26 | + | Collections.sort(list, new MyComp()); |
27 | - | } |
27 | + | System.out.println(list); |
28 | - | |
28 | + | } |
29 | - | Collections.sort(list, new MyComp()); |
29 | + | |
30 | - | System.out.println(list); |
30 | + | static class MyComp implements Comparator<Integer> { |
31 | @Override | |
32 | - | |
32 | + | public int compare(final Integer o1, final Integer o2) { |
33 | - | static class MyComp implements Comparator<Integer> |
33 | + | final int count1 = map.get(o1); |
34 | - | { |
34 | + | final int count2 = map.get(o2); |
35 | - | public int compare(Integer o1, Integer o2) { |
35 | + | |
36 | - | int count1 = map.get(o1); |
36 | + | if (count1 > count2) { |
37 | - | int count2 = map.get(o2); |
37 | + | return -1; |
38 | - | |
38 | + | } else if (count1 < count2) { |
39 | - | if(count1 > count2) |
39 | + | return 1; |
40 | - | { |
40 | + | } else { |
41 | - | return -1; |
41 | + | return 0; |
42 | - | } |
42 | + | } |
43 | - | else if(count1 < count2) |
43 | + | |
44 | - | { |
44 | + | } |
45 | - | return 1; |
45 | + |