Created
January 2, 2017 09:52
-
-
Save niisar/73755a0518b00fc64ce8de670f2307c9 to your computer and use it in GitHub Desktop.
find duplicate in JavaScript
This file contains hidden or 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
//this prototype function checks if an element is already in the array or not | |
//go through all the array and push the element to result if it is not | |
//this way we can eliminate duplicates | |
//result will contain the resultant array | |
Array.prototype.contains = function(k) { | |
for ( var p in this) | |
if (this[p] === k) | |
return true; | |
return false; | |
}; | |
function findDuplicates(Numbers) { | |
var arrayLength = Numbers.length, i, j, result = []; | |
for (i = 0; i < arrayLength; i++) { | |
for (j = 0; j < arrayLength; j++) { | |
if (a[i] == a[j] && i != j && !result.contains(a[i])) { | |
result.push(a[i]); | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment