diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java index 5424e14c72fd..5d380456fd7d 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java @@ -1,16 +1,73 @@ package com.thealgorithms.datastructures.hashmap.hashing; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Optional; /* -This class finds the majority element(s) in an array of integers. +This class finds the majority element(s) in an array/List of integers. A majority element is an element that appears more than or equal to n/2 times, where n is the length of the array. */ public final class MajorityElement { - private MajorityElement() { + private MajorityElement(){} + + /** + * This method uses Moore's voting algorithm to find the majority element in the list. + * If a majority element is present, it returns the element; otherwise, it returns Optional.empty(). + * @param The type of elements in the list + * @param elements List of any type + * @return Optional of majority element or empty if none found + */ + public static Optional majorityElement(List elements) { + if (elements == null || elements.isEmpty()) { + return Optional.empty(); // Handle empty list case + } + + T currentElement = elements.get(0); + long frequency = 1; + + // Moore's Voting Algorithm to find potential majority element + for (int i = 1; i < elements.size(); i++) { + if (frequency == 0) { + currentElement = elements.get(i); + frequency = 1; + } else if (!currentElement.equals(elements.get(i))) { + frequency--; + } else { + frequency++; + } + } + + // Second pass to confirm if it occurs more than n/2 times + long count = 0; + for (T element : elements) { + if (element.equals(currentElement)) { + count++; + } + } + + if (count > elements.size() / 2) { + return Optional.of(currentElement); + } else { + return Optional.empty(); + } + } + + /** + * Method to handle array input and convert to list. + * @param The type of elements in the array + * @param elements Array of any type + * @return Optional of majority element or empty if none found + */ + public static Optional majorityElement(T[] elements) { + if (elements == null || elements.length == 0) { + return Optional.empty(); // Handle empty array case + } + return majorityElement(Arrays.asList(elements)); } + /* This method returns the majority element(s) in the given array of integers. @param nums: an array of integers