Leetcode: 169. Majority Element-shortest solution ever #shorts #ytshort #shorts #ytshort #shorts

preview_player
Показать описание
Problem Description: Majority Element
Problem Statement:
Given an array of size n, return the majority element. The majority element is the element that appears more than floor(n / 2) times in the array. You may assume that the majority element always exists in the array.

Example 1:

Input: nums = [3, 2, 3]
Output: 3

Example 2:

Input: nums = [2, 2, 1, 1, 1, 2, 2]
Output: 2

Explanation
Majority Element:

The majority element is defined as the element that appears more than floor(n / 2) times in the array.
For example, in the array [2, 2, 1, 1, 1, 2, 2], the length of the array is 7. The floor of 7 / 2 is 3. Thus, any element that appears more than 3 times is considered the majority element. In this case, 2 appears 4 times, which is more than 3, so 2 is the majority element.
Guarantee:

The problem guarantees that a majority element always exists. This means you don't need to handle cases where no majority element is present.
Рекомендации по теме