Skip to main content
Back to problems
#3852
Easy Algorithms

Smallest pair with different frequencies

Array Hash Table Counting
69.3% acceptance
Mar 16, 2026
28
2
You are given an integer array nums. Consider all pairs of distinct values x and y from nums such that: x < y x and y have different frequencies in nums. Among all such pairs: Choose the pair with the smallest possible value of x. If multiple pairs have the same x, choose the one with the smallest possible value of y. Return an integer array [x, y]. If no valid pair exists, return [-1, -1].

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn min_distinct_freq_pair(nums: Vec<i32>) -> Vec<i32> {
    use std::collections::HashMap;
    let mut freq: HashMap<i32, i32> = HashMap::new();
    for &x in &nums {
      *freq.entry(x).or_insert(0) += 1;
    }
    let mut vals: Vec<i32> = freq.keys().cloned().collect();
    vals.sort();
    for i in 0..vals.len() {
      for j in (i + 1)..vals.len() {
        if freq[&vals[i]] != freq[&vals[j]] {
          return vec![vals[i], vals[j]];
        }
      }
    }
    vec![-1, -1]
  }
}