mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
style: use getOrDefault
in MajorityElement
(#5455)
This commit is contained in:
@ -19,8 +19,7 @@ public final class MajorityElement {
|
|||||||
public static List<Integer> majority(int[] nums) {
|
public static List<Integer> majority(int[] nums) {
|
||||||
HashMap<Integer, Integer> numToCount = new HashMap<>();
|
HashMap<Integer, Integer> numToCount = new HashMap<>();
|
||||||
for (final var num : nums) {
|
for (final var num : nums) {
|
||||||
final var curCount = numToCount.getOrDefault(num, 0);
|
numToCount.merge(num, 1, Integer::sum);
|
||||||
numToCount.put(num, curCount + 1);
|
|
||||||
}
|
}
|
||||||
List<Integer> majorityElements = new ArrayList<>();
|
List<Integer> majorityElements = new ArrayList<>();
|
||||||
for (final var entry : numToCount.entrySet()) {
|
for (final var entry : numToCount.entrySet()) {
|
||||||
|
Reference in New Issue
Block a user