Skip to main content
Back to problems
#1207
Easy Algorithms

Unique number of occurrences

Array Hash Table
78.6% acceptance
Feb 25, 2026
5543
155
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn unique_occurrences(arr: Vec<i32>) -> bool {
    use std::collections::{HashMap, HashSet};
    let mut counts: HashMap<i32, i32> = HashMap::new();
    for x in arr {
      *counts.entry(x).or_insert(0) += 1;
    }
    let vals: Vec<i32> = counts.values().cloned().collect();
    let unique: HashSet<i32> = vals.iter().cloned().collect();
    vals.len() == unique.len()
  }
}