Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.fhnw.claudemartin;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.TreeMap;
- public class SomeClass {
- public static void main(final String[] arrg) {
- final int[] arr = { 2, 5, 2, 8, 5, 6, 8, 8 };
- System.out.println(Arrays.toString(sortByFrequency(arr)));
- }
- static final int[] sortByFrequency(final int[] input) {
- final Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
- for (final int i : input) {
- if (map.get(i) == null) {
- map.put(i, 1);
- } else {
- int count = map.get(i);
- map.put(i, ++count);
- }
- }
- final List<Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
- list.sort(Collections.reverseOrder(Entry.comparingByValue()));
- return list.stream().mapToInt(e -> e.getKey()).toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement