Majority Element (>N/2)

Greedy & Kadane's Algorithm DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Find the majority element in an array (element occurring more than N/2 times), Arrays, Loops, Frequency Counting, Map, Hash Tables, Time Complexity, Space Complexity, Big O Notation, brute force, frequency counting, array traversal, moore's voting algorithm, time complexity analysis, Moore's Voting Algorithm.

Majority Element Problem Statement Given an array nums of N integers, find and return an element that occurs more than N/2 times in the given array. You may assume that such an element always exists in the array. Input Specification The first line contains an integer N, the size of the array. The second line contains N space separated integers representing the elements of the array nums. Output Specification Return the majority element. Constraints It is guaranteed that a majority element always exists in the array. Sample Test Cases Example 1: Input: Output: Explanation: The number 3 occurs 2 times, which is more than N/2 = 3/2 = 1. Therefore, 3 is the majority element. Example 2: Input: Output: Explanation: The number 2 occurs 4 times, which is more than N/2 = 7/2 = 3. Therefore, 2 is the majority element. Example 3: Input: Output: Explanation: The number 4 occurs 6 times, which is more than N/2 = 10/2 = 5. Therefore, 4 is the majority element.