Skip to content

Commit ed99692

Browse files
committed
feat(other): add count_values
1 parent 19b4ced commit ed99692

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

other/count_values.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const countValues = <T>(it: Iterable<T>) => {
2+
const counts: Map<T, bigint> = new Map()
3+
for (const x of it) {
4+
counts.set(x, (counts.get(x) ?? 0n) + 1n)
5+
}
6+
return counts
7+
}
8+
9+
export const countValuesBounded = <T>(a: readonly T[]) =>
10+
a.reduce(
11+
(counts, x) => counts.set(x, (counts.get(x) ?? 0) + 1),
12+
new Map<T, number>()
13+
)

0 commit comments

Comments
 (0)