Wow๐, this is the third article in this interesting series.
I was afraid of starting this series and sharing my journey or what I am solving because of fear and what people will say but I remembered a quote by Roy T. Bennett
Do not fear failure but rather fear not trying. - Roy T. Bennett
In this article you will learn how to solve the Contains Duplicate Question on Leetcode and I will solve the question using 2 methods which are Hash Tables and the Set Method in JavaScript.
There are a lot of solutions but these two methods shows how to solve it without using inbuilt functions and inbuilt functions like Set.
Let's get inโจ
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:
- 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
Solution (Logic)
Using Hash Tables
I will explain this method with few steps
- Create an empty object and name it
obj
to store thenums
array values.let obj = {}
- Loop through the
nums
array and set every value in theobj
object. - The next step is to check every
obj
key to know if it has occurred before, so that we can return true or set the value to 1 if it has not occurred.
for(let i = 0 ; i < nums.length ; i++){
if(obj[nums[i]]) {
return true;
}
else {
obj[nums[i]] = 1
}
}
- The Step above returns true if it is has a duplicate value.
- The final step is to return false in the main function if no condition is met in the loop.
let obj = {}
for(let i = 0 ; i < nums.length ; i++){
if(obj[nums[i]]) {
return true;
}
else {
obj[nums[i]] = 1
}
}
return false;
This Method is so straight forward and easy to understand.
Set() Method
This method is also very fast and easy to implement.
Note
The Set object lets you store unique values of any type, whether primitive values or object references.
To solve this we will use just 2 steps.
- store the unique values of nums in the Set and spread the values to a new array called
mySet
let mySet = [...new Set(nums)];
Compare the length of this new array
mySet
and thenums
array. If the length of both array are equal then no duplicate but if the length of the two arrays are different, then there is a duplicate value in the array.Return true if the length of the two array is different.
if (mySet.length != nums.length) return true;
Finally if no condition is met, return false.
let mySet = [...new Set(nums)]; if (mySet.length < nums.length) return true; return false;
The Time and Space Complexity
Time Complexity
This Solution has a Time complexity of O(n) which is a Linear time since we are looping through an array of nums and the time depends on the length of the array for method 1 and for the second method we are setting a unique value from the nums array and it also depends on the size or length of nums.
Space complexity
The Space Complexity is also O(n) since we are using extra space to store the words in an object obj
for method 1 and the extra space to store the values in the mySet
Array for method 2.
The Runtime is 76ms, faster than 93.58% of JavaScript online submissions for Contains Duplicate and Memory Usage: 45.2 MB, less than 44.83% of JavaScript online submissions.
Conclusion
I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter here . Watch out for more helpful contents on Data Structures and Algorithm from me.
Don't forget to Like, share and subscribe for more articles in this series called Solutions to Data Structures and Algorithm Questions on Leetcode.
Enjoy ๐