You are given an array nums of size n.
Your task is to identify and return the majority element from this array.
You may assume that:
nums will always be non-empty.n / 2 times.Return the majority element.
[1, 2, 1]
1
The array contains 2 ones and 1 two. The length is 3. n/2 = 1.5. The number 1 appears 2 times, which is more than 1.5.
[2, 2, 1, 1, 1, 2, 2]
2
The array contains 4 twos and 3 ones. The length is 7. n/2 = 3.5. The number 2 appears 4 times, which is more than 3.5.
[3, 3, 4, 5, 4, 5, 2, 1, 3, 3, 3]
3
The array contains 5 threes, 2 fours, 2 fives, 1 two, and 1 one. The length is 11. n/2 = 5.5. The number 3 appears 5 times, which is more than 5.5.
[1, 2, 1]
1