Contains Duplicate(LeetCode)
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.
Examples​
Example 1:
Input: nums = [1,2,3,1]
Output: true
Example 2:
Input: nums = [1,2,3,4]
Output: false
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
Solution​
When tasked with checking for duplicates in an array, there are several approaches to consider. Below, we detail four different methods, from the simplest brute force solution to more optimized approaches using sorting and hash-based structures.
Approach 1: Brute Force​
Algorithm​
- Iterate through the array with an outer loop.
- For each element in the outer loop, iterate through the array with an inner loop starting from the next element.
- Compare the current element of the outer loop with the current element of the inner loop.
- If a match is found, return true.
- If no duplicates are found after all iterations, return false.
Implementation​
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] == nums[j])
return true;
}
}
return false;
}
};
Complexity Analysis​
- Time complexity:
- Space complexity:
Approach 2: Sorting​
Algorithm​
- Sort the array in ascending order.
- Iterate through the sorted array.
- Compare each element with its next element.
- If a match is found, return true.
- If no duplicates are found after all comparisons, return false.
Implementation​
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 1; i < n; i++) {
if (nums[i] == nums[i - 1])
return true;
}
return false;
}
};
Complexity Analysis​
- Time complexity:
- Space complexity:
Approach 3: Hash Set​
Algorithm​
- Create an empty hash set.
- Iterate through the array.
- For each element, check if it exists in the hash set.
- If it exists, return true.
- If it does not exist, add the element to the hash set.
- If no duplicates are found after all iterations, return false.
Implementation​
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> seen;
for (int num : nums) {
if (seen.count(num) > 0)
return true;
seen.insert(num);
}
return false;
}
};
Complexity Analysis​
- Time complexity:
- Space complexity:
Approach 4: Hash Map​
Algorithm​
- Create an empty hash map.
- Iterate through the array.
- For each element, check if it exists in the hash map with a count greater than or equal to 1.
- If it exists, return true. 5.If it does not exist, add the element to the hash map and set its count to 1.
- If no duplicates are found after all iterations, return false.
Implementation​
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> seen;
for (int num : nums) {
if (seen[num] >= 1)
return true;
seen[num]++;
}
return false;
}
};
Complexity Analysis​
- Time complexity:
- Space complexity:
Conclusion​
For checking duplicates in an array, the most efficient approaches are those utilizing hash-based data structures, such as hash sets or hash maps. These methods provide linear time complexity and are suitable for large datasets. While the brute force and sorting approaches are simpler, they are less efficient and may not be practical for larger arrays.