mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Enhance docs, add tests in MajorityElement
(#5978)
This commit is contained in:
@ -3,18 +3,22 @@ package com.thealgorithms.datastructures.hashmap.hashing;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
/*
|
|
||||||
This class finds the majority element(s) in an array of integers.
|
/**
|
||||||
A majority element is an element that appears more than or equal to n/2 times, where n is the length
|
* This class provides a method to find the majority element(s) in an array of integers.
|
||||||
of the array.
|
* A majority element is defined as an element that appears at least ⌊n/2⌋ times,
|
||||||
*/
|
* where n is the length of the array. If multiple elements qualify as majority elements,
|
||||||
|
* they are all returned in a list.
|
||||||
|
*/
|
||||||
public final class MajorityElement {
|
public final class MajorityElement {
|
||||||
private MajorityElement() {
|
private MajorityElement() {
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
This method returns the majority element(s) in the given array of integers.
|
/**
|
||||||
@param nums: an array of integers
|
* Returns a list of majority element(s) from the given array of integers.
|
||||||
@return a list of majority elements
|
*
|
||||||
|
* @param nums an array of integers
|
||||||
|
* @return a list containing the majority element(s); returns an empty list if none exist
|
||||||
*/
|
*/
|
||||||
public static List<Integer> majority(int[] nums) {
|
public static List<Integer> majority(int[] nums) {
|
||||||
HashMap<Integer, Integer> numToCount = new HashMap<>();
|
HashMap<Integer, Integer> numToCount = new HashMap<>();
|
||||||
|
@ -42,4 +42,39 @@ public class MajorityElementTest {
|
|||||||
List<Integer> actual = MajorityElement.majority(nums);
|
List<Integer> actual = MajorityElement.majority(nums);
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMajorityWithAllElementsSame() {
|
||||||
|
int[] nums = {5, 5, 5, 5, 5};
|
||||||
|
List<Integer> expected = new ArrayList<>();
|
||||||
|
expected.add(5);
|
||||||
|
List<Integer> actual = MajorityElement.majority(nums);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMajorityWithEvenCountAndOneMajorityElement() {
|
||||||
|
int[] nums = {1, 2, 2, 3, 3, 2};
|
||||||
|
List<Integer> expected = new ArrayList<>();
|
||||||
|
expected.add(2);
|
||||||
|
List<Integer> actual = MajorityElement.majority(nums);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMajorityWithNoElementsEqualToHalf() {
|
||||||
|
int[] nums = {1, 1, 2, 2, 3, 3, 4};
|
||||||
|
List<Integer> expected = Collections.emptyList();
|
||||||
|
List<Integer> actual = MajorityElement.majority(nums);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMajorityWithLargeArray() {
|
||||||
|
int[] nums = {1, 2, 3, 1, 1, 1, 2, 1, 1};
|
||||||
|
List<Integer> expected = new ArrayList<>();
|
||||||
|
expected.add(1);
|
||||||
|
List<Integer> actual = MajorityElement.majority(nums);
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user