π§π»βπ» LC 169. Majority Element
Given an array num of size n, return the majority element. The majority element is the element that appears more than βn / 2β times.
Input: nums = [2,2,1,1,1,2,2]
Output: 2
The difficulty was in solving it in linear time O(n) and constant space O(1).
Solution: Use Boyer-Moore Voting Algorithm, choose a candidate element and +1 if same -1 if different if 0 then change candidate element.
Input: nums = [2,2,1,1,1,2,2]
Output: 2
The difficulty was in solving it in linear time O(n) and constant space O(1).
Solution: Use Boyer-Moore Voting Algorithm, choose a candidate element and +1 if same -1 if different if 0 then change candidate element.