g1701_1800.s1748_sum_of_unique_elements.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java11 Show documentation
Show all versions of leetcode-in-java11 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
The newest version!
package g1701_1800.s1748_sum_of_unique_elements;
// #Easy #Array #Hash_Table #Counting #2022_04_30_Time_2_ms_(54.08%)_Space_42_MB_(38.95%)
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int sumOfUnique(int[] nums) {
Map map = new HashMap<>();
int sum = 0;
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() == 1) {
sum += entry.getKey();
}
}
return sum;
}
}