We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
getOrDefault
MajorityElement
1 parent 7bde152 commit a49996fCopy full SHA for a49996f
src/main/java/com/thealgorithms/datastructures/hashmap/hashing/MajorityElement.java
@@ -19,8 +19,7 @@ This method returns the majority element(s) in the given array of integers.
19
public static List<Integer> majority(int[] nums) {
20
HashMap<Integer, Integer> numToCount = new HashMap<>();
21
for (final var num : nums) {
22
- final var curCount = numToCount.getOrDefault(num, 0);
23
- numToCount.put(num, curCount + 1);
+ numToCount.merge(num, 1, Integer::sum);
24
}
25
List<Integer> majorityElements = new ArrayList<>();
26
for (final var entry : numToCount.entrySet()) {
0 commit comments