#2442
Medium Algorithms Count number of distinct integers after reverse operations
Array Hash Table Math Counting
81.3% acceptance
Feb 25, 2026
742
60
You are given an array nums consisting of positive integers.
You have to take each integer in the array, reverse its digits, and add it to
the end of the array. You should apply this operation to the original integers in nums. * Return the number of distinct integers in the final array.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn count_distinct_integers(nums: Vec<i32>) -> i32 {
use std::collections::HashSet;
let mut set: HashSet<i32> = nums.iter().cloned().collect();
for &n in &nums {
let mut x = n;
let mut rev = 0;
while x > 0 {
rev = rev * 10 + x % 10;
x /= 10;
}
set.insert(rev);
}
set.len() as i32
}
}