Created
August 29, 2013 05:24
-
-
Save vasco3/6374528 to your computer and use it in GitHub Desktop.
Calculate how many unique absolute numbers are in an array of numbers
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
/* | |
Find the total amount of absolute values | |
*/ | |
function huntBats(b){ | |
console.log(b + " = "); | |
function detect(b){ | |
for( var i = 0 ; i < b.length ; i++ ){ | |
if( b[i]<0) b[i] = b[i] * -1; //normalize | |
for( var j = i+1; j < b.length; j++){ | |
if( b[j] < 0 ) b[j] = b[j] * -1; //normalize | |
if( b[j] === b[i] ) b.splice(j,1); | |
} | |
} | |
return b; | |
} | |
b0 = b; | |
var b1 = detect(b0); | |
var b2 = detect(b1); | |
console.log(b + " = " + b2.length); | |
return b2.length; | |
} | |
//Tests | |
var bG1 = [1,2,-3,4]; //4 | |
var bG2 = [-1,1,-1,1]; //1 | |
var bG3 = [13,-5,3,4,5]; //4 | |
var bG4 = [4,4,4,4]; //1 | |
var bG5 = [5]; //1 | |
huntBats(bG1); | |
huntBats(bG2); | |
huntBats(bG3); | |
huntBats(bG4); | |
huntBats(bG5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment