Python solution using Counter


  • 0
    Q
    class Solution(object):
        def findPairs(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: int
            """
            from collections import Counter
            if k < 0 or not nums:
                return 0
    
            n_c = Counter(nums)
            if not k:
                return sum(v>1 for v in n_c.values())
            else:
                m_c = Counter([v+k for v in nums])
                return len(m_c & n_c)
    

Log in to reply
 

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