From ed996924bdc1accad5ccd24444faabdf1edf7353 Mon Sep 17 00:00:00 2001 From: Rudxain <76864299+Rudxain@users.noreply.github.com> Date: Sat, 27 Dec 2025 13:33:35 -0400 Subject: [PATCH] feat(other): add `count_values` --- other/count_values.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 other/count_values.ts diff --git a/other/count_values.ts b/other/count_values.ts new file mode 100644 index 00000000..f61d8ff5 --- /dev/null +++ b/other/count_values.ts @@ -0,0 +1,13 @@ +export const countValues = (it: Iterable) => { + const counts: Map = new Map() + for (const x of it) { + counts.set(x, (counts.get(x) ?? 0n) + 1n) + } + return counts +} + +export const countValuesBounded = (a: readonly T[]) => + a.reduce( + (counts, x) => counts.set(x, (counts.get(x) ?? 0) + 1), + new Map() + )