Solution of Contains Duplicate in JavaScript

Olamilekan is a software engineer with 2+ years of professional experience in the tech industry, I have a strong background in Web development, working with technologies such as HTML, CSS, Tailwind, JavaScript, TypeScript, React, Redux, Next.js, Node.js, MongoDB etc
In addition to my technical skills, I also have experience in technical writing and I am able to clearly communicate complex technical concepts to both technical and non-technical audiences.
I am constantly keeping up with the latest industry developments and I am always eager to learn new technologies.
When I am not coding I spend time on YouTube watching videos about different Gadgets, Workspace set up, Cars, Smart Homes and many other interesting areas of life 😁
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
objto store thenumsarray values.let obj = {} - Loop through the
numsarray and set every value in theobjobject. - The next step is to check every
objkey 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
mySetlet mySet = [...new Set(nums)]; Compare the length of this new array
mySetand thenumsarray. 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 🎉



