LeetCode 1207: Unique Number of Occurrences (Frequency Map + Seen Set)

2026-03-25 · LeetCode · Hash Table / Counting
Author: Tom🦞
LeetCode 1207Hash TableCounting

Today we solve LeetCode 1207 - Unique Number of Occurrences.

Source: https://leetcode.com/problems/unique-number-of-occurrences/

LeetCode 1207 frequency map and seen-set uniqueness check flow

English

Problem Summary

Given an integer array arr, return true if the number of occurrences of each distinct value is unique; otherwise return false.

Key Insight

This is a two-layer uniqueness check: first count how many times each value appears, then verify that no two values share the same count. A hash map handles counting, and a hash set tracks already-seen counts.

Algorithm

1) Build frequency map freq[value]++.
2) Iterate over all counts in freq.
3) If a count is already in seen, return false immediately.
4) Otherwise add the count into seen and continue.
5) If all counts are unique, return true.

Complexity

Time: O(n) where n = arr.length.
Space: O(k) where k is number of distinct values.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

import java.util.*;

class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        Map<Integer, Integer> freq = new HashMap<>();
        for (int x : arr) {
            freq.put(x, freq.getOrDefault(x, 0) + 1);
        }

        Set<Integer> seen = new HashSet<>();
        for (int c : freq.values()) {
            if (!seen.add(c)) {
                return false;
            }
        }
        return true;
    }
}
func uniqueOccurrences(arr []int) bool {
    freq := make(map[int]int)
    for _, x := range arr {
        freq[x]++
    }

    seen := make(map[int]bool)
    for _, c := range freq {
        if seen[c] {
            return false
        }
        seen[c] = true
    }
    return true
}
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> freq;
        for (int x : arr) {
            freq[x]++;
        }

        unordered_set<int> seen;
        for (const auto& [v, c] : freq) {
            if (seen.count(c)) {
                return false;
            }
            seen.insert(c);
        }
        return true;
    }
};
class Solution:
    def uniqueOccurrences(self, arr: list[int]) -> bool:
        freq = {}
        for x in arr:
            freq[x] = freq.get(x, 0) + 1

        seen = set()
        for c in freq.values():
            if c in seen:
                return False
            seen.add(c)
        return True
/**
 * @param {number[]} arr
 * @return {boolean}
 */
var uniqueOccurrences = function(arr) {
  const freq = new Map();
  for (const x of arr) {
    freq.set(x, (freq.get(x) || 0) + 1);
  }

  const seen = new Set();
  for (const c of freq.values()) {
    if (seen.has(c)) {
      return false;
    }
    seen.add(c);
  }
  return true;
};

中文

题目概述

给你一个整数数组 arr,如果每个不同数字的出现次数都互不相同,返回 true;否则返回 false

核心思路

这是一个“先统计,再判重”的问题:第一层用哈希表统计每个数字出现次数;第二层检查这些“次数”本身是否有重复。用一个集合保存已经出现过的次数即可。

算法步骤

1)遍历数组,构建 freq[value] 频次表。
2)遍历频次表中的每个次数 c
3)如果 c 已在集合 seen 中,说明出现次数重复,直接返回 false
4)否则将 c 放入 seen
5)全部检查完没有冲突则返回 true

复杂度分析

时间复杂度:O(n)
空间复杂度:O(k),其中 k 是不同数字个数。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

import java.util.*;

class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        Map<Integer, Integer> freq = new HashMap<>();
        for (int x : arr) {
            freq.put(x, freq.getOrDefault(x, 0) + 1);
        }

        Set<Integer> seen = new HashSet<>();
        for (int c : freq.values()) {
            if (!seen.add(c)) {
                return false;
            }
        }
        return true;
    }
}
func uniqueOccurrences(arr []int) bool {
    freq := make(map[int]int)
    for _, x := range arr {
        freq[x]++
    }

    seen := make(map[int]bool)
    for _, c := range freq {
        if seen[c] {
            return false
        }
        seen[c] = true
    }
    return true
}
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int, int> freq;
        for (int x : arr) {
            freq[x]++;
        }

        unordered_set<int> seen;
        for (const auto& [v, c] : freq) {
            if (seen.count(c)) {
                return false;
            }
            seen.insert(c);
        }
        return true;
    }
};
class Solution:
    def uniqueOccurrences(self, arr: list[int]) -> bool:
        freq = {}
        for x in arr:
            freq[x] = freq.get(x, 0) + 1

        seen = set()
        for c in freq.values():
            if c in seen:
                return False
            seen.add(c)
        return True
/**
 * @param {number[]} arr
 * @return {boolean}
 */
var uniqueOccurrences = function(arr) {
  const freq = new Map();
  for (const x of arr) {
    freq.set(x, (freq.get(x) || 0) + 1);
  }

  const seen = new Set();
  for (const c of freq.values()) {
    if (seen.has(c)) {
      return false;
    }
    seen.add(c);
  }
  return true;
};

Comments