Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Last active October 22, 2015 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpablocs/3fd0840bc9a6c61f822c to your computer and use it in GitHub Desktop.
Save juanpablocs/3fd0840bc9a6c61f822c to your computer and use it in GitHub Desktop.
Count number by coincidences into an array (Javascript)
/*
@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