Java 8 single chain


  • -2
    A

    public class Solution {
    public List<Integer> topKFrequent(int[] nums, int k) {
    return Arrays.stream(nums)
    .boxed()
    .collect(Collectors.toMap(w -> w, w -> 1, Integer::sum))
    .entrySet()
    .stream()
    .sorted((e1, e2) -> Integer.compare(e2.getValue(),
    e1.getValue())).map(e -> e.getKey()).limit(k).collect(Collectors.toList());
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.