Photo by JESHOOTS.COM on Unsplash
JavaScript LeetCode Contains Duplicate
The second article in my series of solving LeetCode problems with JavaScript. This time we're looking at removing duplicates from an array.
Introduction
Continuing through the problems of LeetCode. I am not haphazardly selecting any questions. I am following along with this list for those that were curious:
techinterviewhandbook.org/best-practice-que..
Prompt
Given an integer array
nums
, returntrue
if any value appears at least twice in the array, and returnfalse
if every element is distinct.
Example 1:
Input: nums = [1,2,3,1]
Output: true
At first glance, the problem seems to be pretty simple. The idea here is to iterate over the array and find any duplicates. There's a bit of a cheat code we can use with JavaScript in this one.
First Solution (cheating)
There is a standard built-in object for JavaScript called [Set] (developer.mozilla.org/en-US/docs/Web/JavaSc..)
But what does this object do?
Set
objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in theSet
's collection.
Knowing this, the only thing we need to do is convert our array into a Set
and compare its length with the original number
array length.
var containsDuplicate = function(nums) {
const set = new Set([...nums]);
return set.size != nums.length;
};
Converting an array into a Set
is simple, just spread it in a new array in the Set
constructor. It has a property to calculate the size (number of items). We just have to compare that with the length of the original array, nums
.
return set.size != nums.length;
Second Solution
Much like our (first solution)[relatablecode.com/javascript-leetcode-two-s.. we can create a hash table of our array as we're iterating and evaluate it in place.
var containsDuplicate = function(nums) {
const hashTable = new Map();
for(let i = 0; i < nums.length; i++) {
if(hashTable.has(nums[i])) return true;
else hashTable.set(nums[i], true);
}
return false;
};
To break this down a little bit: we first iterate over the array of numbers. If the map
already has the value then we return true
. We check this with the .has
property of map
s
if(hashtable.has(nums[i]) return true;
Otherwise, we add it to the map
and move on.
else hashTable.set(nums[i], true);
The value is pretty irrelevant as we don't really care much for it. There probably is a data structure more suited here.
Let's connect
More content at Relatable Code
If you liked this feel free to connect with me on LinkedIn or Twitter
Check out my free developer roadmap and weekly tech industry news in my newsletter.