We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
getOrDefault
MajorityElement
1 parent 7bde152 commit e782c7aCopy full SHA for e782c7a
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