Leetcode Daily Question Solution | 11-12-2023 | #coding #leetcodesolutions #shorts

preview_player
Показать описание
arr = [1,2,2,6,6,6,6,7,10]

1. Initialization:

threshold is calculated as (len(arr) // 4) + 1, which is 9 // 4 + 1 = 3 (integer division).
count is initialized to 1.

2. Iteration:

The loop starts from index 1 (for i in range(1, len(arr)):).
At each iteration, it checks if the current element (arr[i]) is equal to the previous element (arr[i - 1]).
If they are equal, it increments the count; otherwise, it resets count to 1.

3. Counting:

As the loop progresses through the array, it keeps track of the count of consecutive elements.

4. Checking:

Whenever the count exceeds or equals the threshold, it returns the current element as it occurs more than 25% of the time.

5. Output:

In the given example, the number 6 occurs more than 25% of the time in the array, specifically 4 times out of 9, so the output will be 6.
Рекомендации по теме
join shbcf.ru