All files / leetCode 0350.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  2x 2x   2x 7x 2x 5x     2x 7x 6x 4x 4x         2x    
export default function intersect(nums1: number[], nums2: number[]): number[] {
  const map: Map<number, number> = new Map<number, number>()
  const result: number[] = []
 
  for (const num of nums1) {
    if (map.has(num))
      map.set(num, (map.get(num) as number) + 1)
    else map.set(num, 1)
  }
 
  for (const num of nums2) {
    if (map.has(num)) {
      if ((map.get(num) as number) > 0) {
        result.push(num)
        map.set(num, (map.get(num) as number) - 1)
      }
    }
  }
 
  return result
}