SHARE
TWEET

MyComp

DulcetAirman Aug 31st, 2016 (edited) 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. import java.util.Set;
  7.  
  8. class MyClass {
  9.   static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>();
  10.  
  11.   public static void main(final String[] arrg) {
  12.     final int[] arr = { 2, 5, 2, 8, 5, 6, 8, 8 };
  13.     map = new HashMap<Integer, Integer>();
  14.     final Set<Integer> set = new HashSet<>();
  15.     for (int i = 0; i < arr.length; i++) {
  16.       if (map.get(arr[i]) == null) {
  17.         map.put(arr[i], 1);
  18.       } else {
  19.         int count = map.get(arr[i]);
  20.         map.put(arr[i], ++count);
  21.       }
  22.  
  23.       set.add(arr[i]);
  24.     }
  25.     final ArrayList<Integer> list = new ArrayList<Integer>(set);
  26.     Collections.sort(list, new MyComp());
  27.     System.out.println(list);
  28.   }
  29.  
  30.   static class MyComp implements Comparator<Integer> {
  31.     @Override
  32.     public int compare(final Integer o1, final Integer o2) {
  33.       final int count1 = map.get(o1);
  34.       final int count2 = map.get(o2);
  35.  
  36.       if (count1 > count2) {
  37.         return -1;
  38.       } else if (count1 < count2) {
  39.         return 1;
  40.       } else {
  41.         return 0;
  42.       }
  43.     }
  44.   }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top