Skip to content

Commit 858721b

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

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

other/count_values.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 countValuesNew = <T>(a: Iterable<T>) =>
10+
Iterator.from(a).reduce(
11+
(counts, x) => counts.set(x, (counts.get(x) ?? 0n) + 1n),
12+
new Map<T, bigint>()
13+
)
14+
15+
export const countValuesBounded = <T>(a: readonly T[]) =>
16+
a.reduce(
17+
(counts, x) => counts.set(x, (counts.get(x) ?? 0) + 1),
18+
new Map<T, number>()
19+
)

0 commit comments

Comments
 (0)