Skip to content

Instantly share code, notes, and snippets.

@twmulloy
Last active September 26, 2017 23:09
Show Gist options
  • Save twmulloy/8d78610a0d11c273992cb87013a68efb to your computer and use it in GitHub Desktop.
Save twmulloy/8d78610a0d11c273992cb87013a68efb to your computer and use it in GitHub Desktop.
var numbers = [1, 5, 17, 4, 9, 3, 1, 17, 32, 5, 3, 27, 9, 18, 3, 12, 67, 18, 32, 1, 19, 21, 1, 17];
var hashMap = {};
var groupArr = [];
var currentNum;
for (var i = 0; i < numbers.length; i++) {
currentNum = numbers[i];
if (typeof hashMap[currentNum] === 'undefined') {
hashMap[currentNum] = 0;
}
hashMap[currentNum] += 1;
}
for (var prop in hashMap) {
if (!hashMap.hasOwnProperty(prop)) {
break;
}
groupArr.push(prop + ':' + hashMap[prop]);
}
document.getElementById("arrayGrouped2").innerHTML = groupArr.join(',');
// => 1:4,3:3,4:1,5:2,9:2,12:1,17:3,18:2,19:1,21:1,27:1,32:2,67:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment