Contains Duplicate
Array, Hash Table
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.
Example 1:
Example 2:
Example 3:
Solutions
The key insight to solve this problem is to realize that we need to keep track of the elements we have already encountered as we iterate through the array. If we encounter an element that we have seen before, we can immediately return true
because we have found a duplicate. If we finish iterating through the array without finding any duplicates, we return false
.
Approach – Brute Force Technique
The brute force approach involves checking each pair of elements in the array. If any pair is equal, return true
. If no pairs are equal, return false
.
Steps
Iterate over the array: The outer loop (index
i
from 0 tonums.Length - 1
) iterates over each element in the array.Check for duplicates: For each element
nums[i]
, the inner loop (indexj
fromi + 1
tonums.Length - 1
) checks all the following elements in the array to see if they are equal tonums[i]
.Return true if a duplicate is found: If
nums[i]
is equal tonums[j]
, it means we have found a duplicate, so we immediately returntrue
.Return false if no duplicates are found: If we finish iterating through the array without returning
true
, it means we have not found any duplicates, so we returnfalse
.
Complexity
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Approach – Sorting Technique
This approach involves sorting the array first, then checking each pair of adjacent elements. If any pair is equal, return true
. If no pairs are equal, return false
.
Steps
Sort the array: The
Array.Sort(nums)
function sorts the array in ascending order. This operation has a time complexity of O(n log n), where n is the number of elements in the array.Iterate over the array: The loop
for (int i = 0; i < nums.Length - 1; i++)
iterates over each element in the array, except the last one. This is because we're comparing each element with its next neighbor, and the last element doesn't have a next neighbor.Check for duplicates: Inside the loop, the
if (nums[i] == nums[i + 1])
statement checks if the current element is equal to the next element. Because the array is sorted, any duplicate elements will be adjacent to each other. So, if the current element is equal to the next element, it means we have found a duplicate, and we immediately returntrue
.Return false if no duplicates are found: If we finish iterating through the array without returning
true
, it means we have not found any duplicates, so we returnfalse
.
Complexity
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Approach – Hash Table Technique
We can use a data structure like a HashSet that allows for efficient insertions and lookups. We iterate through the array and insert each element into the HashSet. If an element is already in the HashSet, we return true
. If we finish iterating through the array without finding any duplicates in the HashSet, we return false
.
Steps
Initialize: Create a new HashSet
set
. A HashSet is a collection of unique elements.Iterate over the array: The
foreach
loop iterates over each elementnum
in the arraynums
.Check for duplicates: Inside the loop, the
if (set.Contains(num))
statement checks ifnum
is already in the HashSetset
. If it is, it means we have found a duplicate, so we immediately returntrue
.Add element to the set: If
num
is not in the set, we add it to the set usingset.Add(num)
. This step is done for each element in the array.Return false if no duplicates are found: If we finish iterating through the array without returning
true
, it means we have not found any duplicates, so we returnfalse
.
Complexity
Time Complexity: O(n)
Auxiliary Space: O(n)
Last updated