NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 49. Group AnagramsMedium
  • 347. Top K Frequent ElementsMedium
  • 1. Two SumEasy
  • 242. Valid AnagramEasy
  • 13. Roman to IntegerEasy

217. Contains Duplicate

Easy
ArrayHash TableSorting
Companies:
Palantir TechnologiesYahooAirbnbPalantir
Answered: Feb 25, 2026
View on LeetCode

Problem Description

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.

Examples

Example 1

Input: nums = [1,2,3,1]
Output: true
Explanation:
The element 1 occurs at the indices 0 and 3.

Example 2

Input: nums = [1,2,3,4]
Output: false
Explanation:
All elements are distinct.

Example 3

Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true

Constraints

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Solutions

Complexity Analysis

Time Complexity:O(n2)

In the worst case, we have to compare each element with every other element in the array.

Space Complexity:O(1)

The brute force approach does not use any additional data structures that grow with the input size.

The brute force approach uses two nested loops to compare each element with every other element in the array. If it finds any two elements that are the same, it returns true. If it finishes checking all pairs without finding any duplicates, it returns false.

Complexity Analysis

Time Complexity:O(n)

In the worst case, we may have to check each element in the array once.

Space Complexity:O(n)

In the worst case, we may need to store all elements in the set.

The hash set approach uses a set data structure to keep track of the numbers we have seen as we iterate through the array. For each number, we check if it is already in the set. If it is, we return true, indicating that a duplicate has been found. If it is not in the set, we add it to the set and continue iterating. If we finish iterating through the array without finding any duplicates, we return false.

Complexity Analysis

Time Complexity:O(nlogn)

The sorting step takes O(nlog⁡n)O(n \log n)O(nlogn) time, and the subsequent iteration takes O(n)O(n)O(n) time.

Space Complexity:O(n)

In the worst case, the sorting algorithm may require O(n)O(n)O(n) space, depending on the implementation. However, if an in-place sorting algorithm is used, the space complexity can be reduced to O(1)O(1)O(1).

The sorting approach first sorts the array, which brings any duplicate elements next to each other. Then, it iterates through the sorted array and checks for adjacent duplicates. If it finds any two adjacent elements that are the same, it returns true. If it finishes iterating through the array without finding any duplicates, it returns false.

Complexity Analysis

Time Complexity:O(n)

We iterate through the array once to create the set, and then compare lengths, which is O(n)O(n)O(n) overall.

Space Complexity:O(n)

In the worst case, we may need to store all elements in the set.

This approach creates a set from the input array, which automatically removes any duplicate elements. Then, it compares the length of the original array with the length of the set. If they are different, it means that there were duplicates in the original array, and we return true. If they are the same, it means that all elements were distinct, and we return false.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Brute Force
O(n2)O(n^{2})O(n2)O(1)O(1)O(1)No extra space neededVery slow for large inputs
Hash Set
O(n)O(n)O(n)O(n)O(n)O(n)Linear time, can exit early when duplicate foundRequires extra space for the set
Sorting
O(nlog⁡n)O(n \log n)O(nlogn)O(n)O(n)O(n)Simple adjacent comparison after sortingSlower than hash-based approaches, may modify input
Hash Set Length Comparison
O(n)O(n)O(n)O(n)O(n)O(n)Most concise — single line of code, leverages built-in SetCannot exit early, always processes entire array

The optimal approach for this problem is Solution 4: Hash Set Length Comparison, which achieves O(n)O(n)O(n) time complexity and O(n)O(n)O(n) space complexity. It is the most concise solution — simply comparing the length of the original array with a Set created from it — making it extremely readable and easy to implement.


Previous196. Delete Duplicate Emails
Next225. Implement Stack using Queues
Related posts
  • 49. Group AnagramsMedium
  • 347. Top K Frequent ElementsMedium
  • 1. Two SumEasy
  • 242. Valid AnagramEasy
  • 13. Roman to IntegerEasy
On this page
9 lines
1class Solution:
2 def containsDuplicate(self, nums: List[int]) -> bool:
3 # Check each element against every other element
4 for i in range(len(nums)):
5 for j in range(i + 1, len(nums)):
6 if nums[i] == nums[j]:
7 return True
8
9 return False
12 lines
1class Solution:
2 def containsDuplicate(self, nums: List[int]) -> bool:
3 # create a set to store seen numbers
4 seen = set()
5
6 for num in nums:
7 if num in seen:
8 return True
9
10 seen.add(num)
11
12 return False
10 lines
1class Solution:
2 def containsDuplicate(self, nums: List[int]) -> bool:
3 nums.sort()
4
5 # Iterate through the sorted array and check for adjacent duplicates
6 for i in range(1, len(nums)):
7 if nums[i] == nums[i - 1]:
8 return True
9
10 return False
4 lines
1class Solution:
2 def containsDuplicate(self, nums: List[int]) -> bool:
3 # comparing the length of the original list with the length of the set created from the list
4 return len(nums) != len(set(nums))