Skip to content

Commit

Permalink
style: use getOrDefault in MajorityElement (#5455)
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 authored Sep 19, 2024
1 parent 7bde152 commit e782c7a
Showing 1 changed file with 1 addition and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ This method returns the majority element(s) in the given array of integers.
public static List<Integer> majority(int[] nums) {
HashMap<Integer, Integer> numToCount = new HashMap<>();
for (final var num : nums) {
final var curCount = numToCount.getOrDefault(num, 0);
numToCount.put(num, curCount + 1);
numToCount.merge(num, 1, Integer::sum);
}
List<Integer> majorityElements = new ArrayList<>();
for (final var entry : numToCount.entrySet()) {
Expand Down

0 comments on commit e782c7a

Please sign in to comment.