Use java 8 lamda

  • 0
    public List<Integer> topKFrequent(int[] nums, int k) {
            Map<Integer, Integer> rankMap = new HashMap<Integer, Integer> ();
            for(int i = 0; i < nums.length; i++) {
                if(!rankMap.containsKey(nums[i])) rankMap.put(nums[i],0);
                rankMap.put(nums[i], rankMap.get(nums[i])+1);
            return rankMap.entrySet().stream()
            .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
            .map(e-> e.getKey())

Log in to reply

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