Skip to content

Commit

Permalink
Add 1207 unique number of occurences
Browse files Browse the repository at this point in the history
  • Loading branch information
escwxyz committed May 2, 2023
1 parent 644712d commit a5e240e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions typescript/src/1207-unique-number-of-occ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function uniqueOccurrences(arr: number[]): boolean {
const map: Map<number, number> = new Map()

arr.forEach((a) => {
if (map.has(a)) {
map.set(a, map.get(a)! + 1)
} else {
map.set(a, 1)
}
})

const set: Set<number> = new Set()

for (const v of map.values()) {
set.add(v)
}

let sum = 0

set.forEach((s) => {
sum += s
})

return sum === arr.length
}

if (import.meta.vitest) {
const { test, expect } = import.meta.vitest
test('test one', () => {
expect(uniqueOccurrences([1, 2, 2, 1, 1, 3])).toBeTruthy()
})
test('test one', () => {
expect(uniqueOccurrences([1, 2])).toBeFalsy()
})
}

0 comments on commit a5e240e

Please sign in to comment.