generated from tpl/obsidian-sample-plugin
16 lines
347 B
TypeScript
16 lines
347 B
TypeScript
export type FrequencyArray<T> = { value: T; count: number }[];
|
|
|
|
export function frequencyArray<T>(arr: T[]): FrequencyArray<T> {
|
|
const map = new Map<T, number>();
|
|
|
|
for (const item of arr) {
|
|
const count = map.get(item) ?? 0;
|
|
map.set(item, count + 1);
|
|
}
|
|
|
|
return Array.from(map.entries()).map(([value, count]) => ({
|
|
value,
|
|
count,
|
|
}));
|
|
}
|