Blog.

独一无二的出现次数

Cover Image for 独一无二的出现次数
Bernie
Bernie

1207. 独一无二的出现次数

leetcode 链接

思路:

1.用 map1 记录每个数字出现的次数。

2.然后再用一个 map2 记录每个次数是否出现过,当记录 map2 时,如果出现该次数已经存在,则直接返回 false,

3.如果遍历完了,则返回 true。

typescript 解法

type Mapp1 = {
  [key: number]: number;
};

function uniqueOccurrences(arr: number[]): boolean {
  const mapp1: Mapp1 = {};
  const counts: number[] = [];

  for (const item of arr) {
    if (!mapp1[item]) {
      mapp1[item] = 1;
    } else {
      mapp1[item] += 1;
    }
  }
  for (const index in mapp1) {
    const count: number = mapp1[index];
    if (counts.includes(count)) {
      return false;
    }
    counts.push(count);
  }
  return true;
}

go 解法

func uniqueOccurrences(arr []int) bool {
    counts := make(map[int]int)
    countsMap := make(map[int]bool)
    for _, v := range arr {
        counts[v] += 1
    }
    for _, v := range counts {
        if countsMap[v] == true {
            return false
        }
        countsMap[v] = true
    }
    return true
}
}