mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Enhance docs, add tests in MajorityElement (#5978)
This commit is contained in:
@@ -42,4 +42,39 @@ public class MajorityElementTest {
|
||||
List<Integer> actual = MajorityElement.majority(nums);
|
||||
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