fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Integer[] input = { 3,5,7,5,3,7,8,4,34,6,8,5,3,6,8,31,0,7,6,0,1,1,1,1,1,1 };
  13. HashMap<Integer, Integer> map = new HashMap<>();
  14. for(Integer i: input) {
  15. Integer count = map.get(i);
  16. if(count == null) {
  17. map.put(i, 1);
  18. } else {
  19. count++;
  20. map.put(i, count);
  21. }
  22. }
  23.  
  24. ArrayList<Integer> values = new ArrayList<>(map.keySet());
  25. Collections.sort(values, Collections.reverseOrder());
  26. ArrayList<Integer> output = new ArrayList<>();
  27. for(Integer value: values) {
  28. addMultipleValue(output, value, map.get(value));
  29. }
  30. System.out.println(output.toString());
  31. }
  32.  
  33. private static void addMultipleValue(ArrayList<Integer> list, Integer value, int time) {
  34. for(int i=0;i<time;i++) {
  35. list.add(value);
  36. }
  37. }
  38. }
Success #stdin #stdout 0.06s 32144KB
stdin
Standard input is empty
stdout
[34, 31, 8, 8, 8, 7, 7, 7, 6, 6, 6, 5, 5, 5, 4, 3, 3, 3, 1, 1, 1, 1, 1, 1, 0, 0]