Skip to main content
Back to problems
#1287
Easy Algorithms

Element appearing more than 25 in sorted array

Array
61.1% acceptance
Feb 25, 2026
1775
84
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_special_integer(arr: Vec<i32>) -> i32 {
    let threshold = arr.len() / 4;
    let mut count = 1;
    for i in 1..arr.len() {
      if arr[i] == arr[i - 1] {
        count += 1;
        if count > threshold {
          return arr[i];
        }
      } else {
        count = 1;
      }
    }
    arr[0]
  }
}