Last active
October 22, 2015 17:50
-
-
Save juanpablocs/3fd0840bc9a6c61f822c to your computer and use it in GitHub Desktop.
Count number by coincidences into an array (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
/* | |
@author: juanpablocs21@gmail.com | |
@param: array (evaluate) | |
@return: array | |
*/ | |
function count_numbers(arr){ | |
var c = {}; | |
var result = []; | |
for(e in arr){ | |
n = arr[e] | |
c[n] = c[n] ? c[n]+1 : 1; | |
} | |
for(i=0;i<10;i++){ | |
result.push(~~c[i]); // ~~ convert undefined to 0 | |
} | |
return result; | |
} | |
console.log(count_numbers([2,2,3,4,5])); //return [0, 0, 2, 1, 1, 1, 0, 0, 0, 0] | |
/* | |
array => | |
2,2,3,4,5 | |
return | |
----- | |
|0 = 0 | |
|1 = 0 | |
|2 = 2 | |
|3 = 1 | |
|4 = 1 | |
|5 = 1 | |
|6 = 0 | |
|7 = 0 | |
|8 = 0 | |
|9 = 0 | |
------ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment