LeetCode 217: Contains Duplicates
An Easy Approach to LeetCode 217: Finding and Managing Duplicates

I'm Varchasv, a Data Engineer working on enterprise data integration.
Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.
What I'm doing:
- Solving 2 Leetcode problems daily (SQL + DSA).
- Blogging about each problem.
- Building in public.
My Goal - Land a better data engineering role by mid-2026.
Follow my journey !!
Date: January 13, 2026
Category: HashMap | Set | Array | Loop
Time Taken: 5 minutes
Difficulty: Easy
Problem Statement
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Link: Contains Duplicate
My Approach
Initial thought:
I could do a nested loop on the list that will check for duplicates, and whenever it finds a duplicate, it will return True; otherwise, False. But this approach will have a time complexity of O(N2) and space complexity of O(1) because I am not storing anything. This is the brute force method.
We can also sort the list first and compare adjacent elements for duplicates. This solution will have a time complexity of O(N LogN) and a space complexity of O(N).
One more solution could be to create a HashMap and check for duplicates while adding num from
numsto the HashMap; if we find any duplicates, we return True; otherwise, False. This solution will have a time complexity of O(N) and a space complexity of O(N), as HashMap has constant lookup time.
Final solution:
The best solution would be to compare the length of nums list and the length of the set of nums list. If they are equal, we return False, as every value is distinct; else, we return True.
Why this works:
Sets are better optimized to check uniqueness.
They have an average lookup time of O(1).
Solution Code (HashMap)
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen= {}
for value in nums:
if value in seen:
return True
seen[value] = 1
return False
Solution Code (Set)
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
Complexity: Time: O(N) Space: O(N)
Key Takeaway: Sets are better for checking the uniqueness of a list.
Pattern: Frequency Map
Mistakes I Made:
I did not think about Set solution at the start.
In the HashMap solution, I counted the occurrences at first and did not look for duplicates in the same for loop.
Series: 90 Days of Data Engineering Progress: 2/90 problems completed
Tags: #LeetCode #SQL #DataEngineering