We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
count_values
1 parent 19b4ced commit ed99692Copy full SHA for ed99692
1 file changed
other/count_values.ts
@@ -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