Find all duplicates in an array

November 20, 2022

array-and-hashmap

Problem URL: Find all duplicates in an array

We will count the frequency of each number in the array, and return the numbers that appear twice.

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        freq = collections.Counter(nums)
        return [num for num, count in freq.items() if count == 2]

Time complexity: O(n)
Space complexity: O(n)