Skip to main content
Back to problems
#2148
Easy Algorithms

Count elements with strictly smaller and greater elements

Array Sorting Counting
59.8% acceptance
Feb 25, 2026
713
46
Given an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_elements(nums: Vec<i32>) -> i32 {
    let &mn = nums.iter().min().unwrap();
    let &mx = nums.iter().max().unwrap();
    nums.iter().filter(|&&x| x > mn && x < mx).count() as i32
  }
}