LeetCode 217 Contains Duplicate
LeetCode 217 - Determine if an array contains duplicate numbers using hash set.
Created Jul 1, 2021 - Last updated: Jul 1, 2021
LeetCode 217: Contains Duplicate
Created: September 19, 2024 5:26 PM Labels & Language: C++, LeetCode, Python
Description
https://leetcode.com/problems/contains-duplicate/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.
LeetCode 217: Contains Duplicate
Approach: Hash Set
The problem is to determine if any integer appears at least twice in the given integer array nums
. To efficiently check for duplicates, we can use a hash set. The main idea is to leverage the properties of a hash set, which allows for fast membership testing and ensures that all elements are unique.
- Initialize a Hash Set
- Create an empty hash set (or unordered set in C++) to store the unique elements encountered while iterating through the array.
- Iterate Through the Array
- Loop through each element in the array
nums
. - For each element, check if it is already present in the hash set.
- Loop through each element in the array
- Check for Duplicates
- If the current element is found in the hash set (using
find
or a similar method), it means that this value has been seen before, and we can immediately returntrue
. - If the current element is not in the hash set, insert it into the set for future checks.
- If the current element is found in the hash set (using
- Return Result
- If the loop completes without finding any duplicates, return
false
, indicating that all elements in the array are distinct.
- If the loop completes without finding any duplicates, return
Complexity
- Time Complexity: O(n)
The algorithm iterates through the array once, and each insertion and lookup operation in the hash set takes average O(1) time. Therefore, the overall complexity is linear with respect to the number of elements in the array.
- Space Complexity: O(n)
In the worst case, if all elements are distinct, we would store all n elements in the hash set, leading to linear space usage.
Solution → C++
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> hashTable;
for (int i = 0; i < nums.size(); i ++) {
if (hashTable.find(nums[i]) != hashTable.end())
{
return true;
}
else
{
hashTable.insert(nums[i]);
}
}
return false;
}
};
Solution → Python
class Solution:
def containsDuplicate(self, nums):
hash_table = set()
for num in nums:
if num in hash_table:
return True
else:
hash_table.add(num)
return False