Skip to content

Instantly share code, notes, and snippets.

@sudhir600
Created August 1, 2019 05:02
Show Gist options
  • Save sudhir600/c65d7b41ef820b69d5baa393548436eb to your computer and use it in GitHub Desktop.
Save sudhir600/c65d7b41ef820b69d5baa393548436eb to your computer and use it in GitHub Desktop.
Get total numbers of same key in array
// E.g
// let array = ["sudhir", "hello", "test", "sudhir", "a", "a", "a"]
//output = sudhir:2, hello:1, test: 1 and a:3
countKey(array){
let obj = {}
array.forEach(function(i){
if(obj[i] == undefined){
obj[i] = 0
}
obj[i] = obj[i] + 1
})
return obj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment