Skip to content

Instantly share code, notes, and snippets.

@Smakar20
Created February 15, 2019 18:35
Show Gist options
  • Save Smakar20/92f5bd5acf37853060a3980e09fb1b6c to your computer and use it in GitHub Desktop.
Save Smakar20/92f5bd5acf37853060a3980e09fb1b6c to your computer and use it in GitHub Desktop.
getMostRepeated.js created by smakar20 - https://repl.it/@smakar20/getMostRepeatedjs
/*
array = [2, 1, 2, 1, 1, 3, 4, 3, 5]
f(array, k) = Return the Top K most repeating integers in the array
f(array, 1) = [1]
f(array, 2) = [1, 2] OR [1, 3]
f(array, 3) = [1, 2, 3] OR [1, 3, 2]
*/
function getMostRepeated(arr, k){
if (arr.length === 0 || k === 0) return 0;
var charMap = {};
for (var i = 0; i < arr.length; i++){
if ((charMap[arr[i]] !== undefined)){
charMap[arr[i]]["count"] += 1;
}else{
charMap[arr[i]] = {};
charMap[arr[i]]["num"] = arr[i];
charMap[arr[i]]["count"] = 1;
}
}
var sortedArr = Object.values(charMap).sort((a,b)=>{ return b.count - a.count});
var i = 0;
var output = [];
while (i < k){
output.push(sortedArr[i].num);
i++;
}
return output;
}
var array = [2, 1, 2, 1, 1, 3, 4, 3, 5];
console.log(getMostRepeated(array, 1));
console.log(getMostRepeated(array, 2));
console.log(getMostRepeated(array, 3));
console.log(getMostRepeated(array, 4));
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"underscore": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.1.tgz",
"integrity": "sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg=="
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment