Skip to content

Instantly share code, notes, and snippets.

@tarzak
Last active August 29, 2015 13:58
Show Gist options
  • Save tarzak/9975768 to your computer and use it in GitHub Desktop.
Save tarzak/9975768 to your computer and use it in GitHub Desktop.
Counting of Array Elements
var arr = [7,3,1,2,2,1,7,1,1,1,4], arrObj = {};
function elementCount(arr) {
var i = 0, prop, name;
for (i = 0; i < arr.length; i++) {
name = arr[i];
if (!arrObj[name]) {
arrObj[name] = 0;
}
for (prop in arrObj)
if (arr[i] === parseInt(prop))
arrObj[prop]++;
}
return arrObj;
}
elementCount(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment