Created
August 22, 2017 06:30
-
-
Save alvachien/6c0e5e9a5c3834e10a17265984a468e5 to your computer and use it in GitHub Desktop.
Check duplicated items in array, with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Array related functions | |
// | |
function eliminateDuplicates(arr) { | |
var i, | |
len=arr.length, | |
out=[], | |
obj={}; | |
for (i=0;i<len;i++) { | |
obj[arr[i]]=0; | |
} | |
for (i in obj) { | |
out.push(i); | |
} | |
return out; | |
} | |
function hasDuplicates(arr) { | |
return (new Set(arr)).size !== arr.length; | |
} | |
function hasDuplicatesInStringArray(strarray) { | |
var valuesSoFar = Object.create(null); | |
for (var i = 0; i < strarray.length; ++i) { | |
var value = strarray[i]; | |
if (value in valuesSoFar) { | |
return true; | |
} | |
valuesSoFar[value] = true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment