Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 580 Bytes

Question_442.md

File metadata and controls

22 lines (19 loc) · 580 Bytes

LeetCode Records - Question 442 Find All Duplicates in an Array

Attempt 1: Use a HashMap

class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>(nums.length);
        for (int i : nums) {
            map.merge(i, 1, Integer::sum);
        }

        return map.entrySet().stream()
                .filter(x -> x.getValue() == 2)
                .map(Map.Entry::getKey)
                .toList();
    }
}
  • Runtime: 22 ms (Beats: 12.91%)
  • Memory: 55.35 MB (Beats: 8.79%)