Skip to content

Commit 4ea3098

Browse files
authored
Enhance docs, add tests in MajorityElement (#5978)
1 parent 7e87e58 commit 4ea3098

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
import java.util.ArrayList;
44
import java.util.HashMap;
55
import java.util.List;
6-
/*
7-
This class finds the majority element(s) in an array of integers.
8-
A majority element is an element that appears more than or equal to n/2 times, where n is the length
9-
of the array.
10-
*/
6+
7+
/**
8+
* This class provides a method to find the majority element(s) in an array of integers.
9+
* A majority element is defined as an element that appears at least ⌊n/2⌋ times,
10+
* where n is the length of the array. If multiple elements qualify as majority elements,
11+
* they are all returned in a list.
12+
*/
1113
public final class MajorityElement {
1214
private MajorityElement() {
1315
}
14-
/*
15-
This method returns the majority element(s) in the given array of integers.
16-
@param nums: an array of integers
17-
@return a list of majority elements
18-
*/
16+
17+
/**
18+
* Returns a list of majority element(s) from the given array of integers.
19+
*
20+
* @param nums an array of integers
21+
* @return a list containing the majority element(s); returns an empty list if none exist
22+
*/
1923
public static List<Integer> majority(int[] nums) {
2024
HashMap<Integer, Integer> numToCount = new HashMap<>();
2125
for (final var num : nums) {

src/test/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElementTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,39 @@ void testMajorityWithEmptyArray() {
4242
List<Integer> actual = MajorityElement.majority(nums);
4343
assertEquals(expected, actual);
4444
}
45+
46+
@Test
47+
void testMajorityWithAllElementsSame() {
48+
int[] nums = {5, 5, 5, 5, 5};
49+
List<Integer> expected = new ArrayList<>();
50+
expected.add(5);
51+
List<Integer> actual = MajorityElement.majority(nums);
52+
assertEquals(expected, actual);
53+
}
54+
55+
@Test
56+
void testMajorityWithEvenCountAndOneMajorityElement() {
57+
int[] nums = {1, 2, 2, 3, 3, 2};
58+
List<Integer> expected = new ArrayList<>();
59+
expected.add(2);
60+
List<Integer> actual = MajorityElement.majority(nums);
61+
assertEquals(expected, actual);
62+
}
63+
64+
@Test
65+
void testMajorityWithNoElementsEqualToHalf() {
66+
int[] nums = {1, 1, 2, 2, 3, 3, 4};
67+
List<Integer> expected = Collections.emptyList();
68+
List<Integer> actual = MajorityElement.majority(nums);
69+
assertEquals(expected, actual);
70+
}
71+
72+
@Test
73+
void testMajorityWithLargeArray() {
74+
int[] nums = {1, 2, 3, 1, 1, 1, 2, 1, 1};
75+
List<Integer> expected = new ArrayList<>();
76+
expected.add(1);
77+
List<Integer> actual = MajorityElement.majority(nums);
78+
assertEquals(expected, actual);
79+
}
4580
}

0 commit comments

Comments
 (0)