Array of size n, with one element n/2 times

前端 未结 9 785
离开以前
离开以前 2021-02-04 12:41

Given an array of n integers, where one element appears more than n/2 times. We need to find that element in linear time and constant extra space.

YAAQ: Yet another arra

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-04 13:16

    Find the median, it takes O(n) on an unsorted array. Since more than n/2 elements are equal to the same value, the median is equal to that value as well.

提交回复
热议问题