Skip to content

Added two additional methods - 217 Contains Duplicates #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions javascript/217-Contains-Duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* @param {number[]} nums
* @return {boolean}
*/

//First method using Map() (exit early if true)
var containsDuplicate = function(nums) {
const numsSet = new Set()
for(const i of nums){
Expand All @@ -10,5 +12,20 @@ var containsDuplicate = function(nums) {
}
numsSet.add(i)
}
return false
};
return false;
};

//Second method using Map() (Has to map entire array but code is more readable)
var containsDuplicate = function(nums) {
//create a new hashmap with all the items in the array. Any duplicates will be removed.
const totalWithoutDuplicates = new Map(nums.map((i) => [i]));

//check if the size of the initial array is larger than the new hashmap.
return totalWithoutDuplicates.size !== nums.length;
};

//Third method using Set() (Fastest runtime at 91.95% and very readable code)
var containsDuplicate = function(nums) {
//Pass the array into a Set() (which removes duplicates) and then compare its size to the original array.
return new Set(nums).size !== nums.length;
};
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy