Skip to main content
Back to problems
#2239
Easy Algorithms

Find closest number to zero

Array
47.8% acceptance
Feb 25, 2026
797
56
Given an integer array nums of size n, return the number with the value closest to 0 in nums. If there are multiple answers, return the number with the largest value.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_closest_number(nums: Vec<i32>) -> i32 {
    nums.into_iter().min_by_key(|&x| (x.abs(), -x)).unwrap()
  }
}