Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- class MyClass
- {
- static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>();
- public static void main(String[] arrg)
- {
- int[] arr = {2,5,2,8,5,6,8,8};
- map = new HashMap<Integer,Integer>();
- ArrayList<Integer> list = new ArrayList<Integer>();
- for(int i=0; i<arr.length; i++)
- {
- if(map.get(arr[i]) == null)
- {
- map.put(arr[i],1);
- }
- else
- {
- int count = map.get(arr[i]);
- map.put(arr[i],++count);
- }
- list.add(arr[i]);
- }
- Collections.sort(list, new MyComp());
- System.out.println(list);
- }
- static class MyComp implements Comparator<Integer>
- {
- public int compare(Integer o1, Integer o2) {
- int count1 = map.get(o1);
- int count2 = map.get(o2);
- if(count1 > count2)
- {
- return -1;
- }
- else if(count1 < count2)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement