DEV Community

WhereIsLijah
WhereIsLijah

Posted on

217. Contains Duplicate

Topic: Array & Hashing

I came up with 2 different solutions

Soln 1:

  • Compare the length of the array with the length of the set created from the array
  • Sets is a data structure that does not contain duplicates elements
return len(nums) != len(set(nums))
Enter fullscreen mode Exit fullscreen mode

Soln 2:

  • Declare a set as a variable
  • loop through the integers in the array
  • if the integer does not exist in the set, add it to the set
  • else, it means a duplicate exists
         count = set()

         for n in nums:
             if n in count:
               return True
             count.add(n)

         return False

Enter fullscreen mode Exit fullscreen mode

Notes: I worked on this before and just needed it to re-introduce myself into leetcoding

Top comments (0)