This page looks best with JavaScript enabled

Contains Duplicate

 ·  ☕ 1 min read  ·  ✍️ Syed Dawood

Problem

LeetCode 217: Contains Duplicate

Solution

1
2
3
4
5
6
7
8
class Solution:
    def containsDuplicate(self, nums: list[int]) -> bool:
        num_set = set()
        for num in nums:
            if num in num_set:
                return True
            num_set.add(num)
        return False

Explaination

We leverage nature of python sets, which gives O(1) for membership check and adding new member, in worst case scenario it is O(n).

References

Also see

Share on

ALLSYED
WRITTEN BY
Syed Dawood
< frontend | backend | fullstack > Developer