Class Solution

java.lang.Object
g0101_0200.s0169_majority_element.Solution

public class Solution extends Object
169 - Majority Element.<p>Easy</p> <p>Given an array <code>nums</code> of size <code>n</code>, return <em>the majority element</em>.</p> <p>The majority element is the element that appears more than <code>\u230an / 2\u230b</code> times. You may assume that the majority element always exists in the array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,2,3]</p> <p><strong>Output:</strong> 3</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,2,1,1,1,2,2]</p> <p><strong>Output:</strong> 2</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 5 * 10<sup>4</sup></code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li> </ul> <p><strong>Follow-up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • majorityElement

      public int majorityElement(int[] arr)