From 16da7fb310198851c2e4b02abedfb24979287242 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 7 Jan 2017 18:26:02 -0400 Subject: Initial commit --- util/count.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 util/count.js (limited to 'util/count.js') diff --git a/util/count.js b/util/count.js new file mode 100644 index 0000000..7df97a7 --- /dev/null +++ b/util/count.js @@ -0,0 +1,17 @@ +module.exports = function count(arr) { + // Counts the number of times the items of an array appear (only on the top + // level; it doesn't search through nested arrays!). Returns a map of + // item -> count. + + const map = new Map() + + for (let item of arr) { + if (map.has(item)) { + map.set(item, map.get(item) + 1) + } else { + map.set(item, 1) + } + } + + return map +} -- cgit 1.3.0-6-gf8a5