/169. Majority Element

169. Majority Element

Easy
Arrays66.2% acceptance

Given an integer array data of length n, return the element that appears strictly more than floor(n / 2) times. It is guaranteed that such an element exists in data.

Example 1

Input: [7,7,5,7]

Output: 7

Explanation: 7 appears 3 times, which is more than floor(4/2)=2.

Example 2

Input: [4,4,4,2,2]

Output: 4

Explanation: 4 appears 3 times, which is more than floor(5/2)=2.

Constraints

  • 1 <= len(data) <= 50000
  • -109 <= data[i] <= 109 for all valid i
  • A majority element exists in data (appears strictly more than floor(len(data)/2) times)
Python (current runtime)

Case 1

Input: [10,10,10,3,3]

Expected: 10

Case 2

Input: [1,2,1,1]

Expected: 1

Case 3

Input: [6,6,6,6,2,2]

Expected: 6

Case 4

Input: [8,8,8,8,8,1,2,3]

Expected: 8