This approach leverages the properties of a HashSet (or similar data structures depending on the programming language), which allows for average O(1) time complexity for insertion and lookup operations. As you iterate over the array, you check if the current element is already in the HashSet. If it is, then a duplicate has been found, and you can return true immediately. If it’s not already in the HashSet, you add it. If no duplicates are found by the end of the array, you return false.
Time Complexity: O(n log n), due to the sorting step.
Space Complexity: O(1), no extra space required apart from sorting.
1using System.Collections.Generic;
2public class Solution {
3 public bool ContainsDuplicate(int[] nums) {
4 HashSet<int> seen = new HashSet<int>();
5 foreach (int num in nums) {
6 if (seen.Contains(num)) {
7 return true;
8 }
9 seen.Add(num);
10 }
11 return false;
12 }
13}
The C# solution uses a HashSet to track numbers. Each number is checked for its existence in the set, and added if not present. This approach is highly efficient for lookup and insertion operations.
This approach involves sorting the array first, then checking for duplicates by comparing each element with its next neighbor. If duplicates exist, they will appear next to each other after sorting.
Time Complexity: O(n log n), due to sorting.
Space Complexity: O(1), aside from sorting in-place.
1#include <vector>
2#include <algorithm>
3bool containsDuplicate(std::vector<int>& nums) {
4 sort(nums.begin(), nums.end());
5 for (int i = 0; i < nums.size() - 1; i++) {
6 if (nums[i] == nums[i + 1]) {
7 return true;
8 }
9 }
10 return false;
11}
For C++, we utilize the standard sort function which sorts the array in-place, followed by a linear sweep to detect duplicates in successive elements.