Last active
November 2, 2015 20:03
-
-
Save poeschko/18d47f567ed9432d0a32 to your computer and use it in GitHub Desktop.
Count distinct items in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countDistinct(items) { | |
var result = []; | |
for (var i = 0, l = items.length; i < l; ++i) { | |
var item = items[i]; | |
var index = -1; | |
for (var j = 0; j < result.length; ++j) { | |
var existing = result[j]; | |
if (item === existing[0]) { | |
index = j; | |
break; | |
} | |
} | |
if (index >= 0) { | |
++result[index][1]; | |
} else { | |
result.push([item, 1]); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment