Python with Counter


  • 0
    T
    class Solution(object):
        def containsNearbyDuplicate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: bool
            """
            from collections import Counter
            num0=Counter(nums)
            for i in num0:
                if num0[i]<2:
                    continue
                else:
                    base=nums.index(i)
                    counter=1
                    while counter<num0[i]:
                        found=nums.index(i,base+1)
                        if found-base<=k:
                            return True
                        base=found
                        counter+=1
            return False
    

Log in to reply
 

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