-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfUniqueElements.java
More file actions
36 lines (26 loc) · 861 Bytes
/
Copy pathSumOfUniqueElements.java
File metadata and controls
36 lines (26 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package LeetcodePractice;
import java.util.HashMap;
import java.util.Map;
public class SumOfUniqueElements {
public static int sumOfUnique(int[] nums) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : nums) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
int sum = 0;
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
if (entry.getValue() == 1) {
sum += entry.getKey();
}
}
return sum;
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 2};
System.out.println(sumOfUnique(nums1));
int[] nums2 = {1, 1, 1, 1, 1};
System.out.println(sumOfUnique(nums2));
int[] nums3 = {1, 2, 3, 4, 5};
System.out.println(sumOfUnique(nums3));
}
}