Majority Element
Last updated
Was this helpful?
Last updated
Was this helpful?
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
利用一個count 計算candidate and non-candidate 出現次數。當為0時切換candidate. Array的 pre candidate count不重要, suffix candidate is sufficient
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times.
Note: The algorithm should run in linear time and in O(1) space.