Isaac Emmanuel
09 Sept 2026•EasyGo / Python / TypeScript

Element Appearing More Than 25% In Sorted Array

Find the value occurring in more than one quarter of a sorted array.

Count each contiguous run in the sorted array and return the first whose length exceeds one quarter of the array.

Complexity

O(n) time and O(1) space.

Solution files

Go•element-appearing-more-than-25-in-sorted-array/solution.go
package main

func findSpecialInteger(arr []int) int {
	run := 1
	for index := 1; index < len(arr); index++ {
		if arr[index] == arr[index-1] {
			run++
		} else {
			run = 1
		}
		if run*4 > len(arr) {
			return arr[index]
		}
	}
	return arr[0]
}
Python•element-appearing-more-than-25-in-sorted-array/solution.py
class Solution:
    def findSpecialInteger(self, arr: list[int]) -> int:
        run = 1
        for index in range(1, len(arr)):
            run = run + 1 if arr[index] == arr[index - 1] else 1
            if run * 4 > len(arr): return arr[index]
        return arr[0]
TypeScript•element-appearing-more-than-25-in-sorted-array/solution.ts
function findSpecialInteger(arr: number[]): number {
  let run = 1;
  for (let index = 1; index < arr.length; index += 1) { run = arr[index] === arr[index - 1] ? run + 1 : 1; if (run * 4 > arr.length) return arr[index]; }
  return arr[0];
}