"A collection of essential coding algorithms and popular code solutions. Optimized for interviews and competitive programming, covering a wide range of topics from sorting to dynamic programming.”
13
stars
194
forks
source link
Implement Finding Majority Element in an Array #295
Implement a Java program that finds the majority element in an array, where the majority element is the one that appears more than n/2 times. If no majority element exists, return -1.
Description:
Implement a Java program that finds the majority element in an array, where the majority element is the one that appears more than n/2 times. If no majority element exists, return -1.
Test Cases:
Input: nums = [2, 2, 1, 1, 1, 2, 2] Expected Output: 2
Input: nums = [3, 3, 4, 2, 4, 4, 2, 4, 4] Expected Output: 4
Input: nums = [3, 2, 2, 1, 8] Expected Output: -1
Constraints:
The input array will always have non-negative integers.