Skip to content

Commit e782c7a

Browse files
authored
style: use getOrDefault in MajorityElement (#5455)
1 parent 7bde152 commit e782c7a

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ This method returns the majority element(s) in the given array of integers.
1919
public static List<Integer> majority(int[] nums) {
2020
HashMap<Integer, Integer> numToCount = new HashMap<>();
2121
for (final var num : nums) {
22-
final var curCount = numToCount.getOrDefault(num, 0);
23-
numToCount.put(num, curCount + 1);
22+
numToCount.merge(num, 1, Integer::sum);
2423
}
2524
List<Integer> majorityElements = new ArrayList<>();
2625
for (final var entry : numToCount.entrySet()) {

0 commit comments

Comments
 (0)