Last active
August 29, 2015 14:05
-
-
Save macikokoro/86654a7319ee5b84f194 to your computer and use it in GitHub Desktop.
Find repeating numbers and eliminate the duplicates.
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
var numArray = [2, 3, 6, 4, 8, 10, 11, 13, 1, 2, 2, 3,3,3,3]; | |
//sort numbers in order | |
numArray.sort(function(a, b) {return a-b;}); | |
//empty array to store results | |
var result = []; | |
//loop through array | |
for (var i = 0; i < numArray.length; i++) { | |
//compare results | |
if (numArray[i + 1] === numArray[i]) { | |
//push duplicates | |
result.push(numArray[i]); | |
} | |
} | |
//log the contents of the array | |
console.log(numArray); | |
//log the repeating numbers in array | |
console.log("these numbers repeat: " + result); | |
//made possible by http://dreaminginjavascript.wordpress.com/ | |
//Eliminating duplicates | |
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; | |
} | |
var b=[]; | |
b=eliminateDuplicates(result); | |
console.log(result); | |
console.log("array without duplicates " + b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment