Skip to content

Instantly share code, notes, and snippets.

@varavut
Last active August 29, 2015 14:27
Show Gist options
  • Save varavut/31ecb9f2c0787a8676c9 to your computer and use it in GitHub Desktop.
Save varavut/31ecb9f2c0787a8676c9 to your computer and use it in GitHub Desktop.
function countElement(array, i, acc){
if(!i)
i = 0;
if(!acc)
acc = {};
if(array.length<=i)
return acc;
if(!acc[array[i]])
acc[array[i]] = 0;
acc[array[i]]++;
return countElement(array, i+1, acc)
}
countElement([1,1,2,2,2,3,4,5,5,6]);
===output===
{ '1': 2, '2': 3, '3': 1, '4': 1, '5': 2, '6': 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment