All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1701_1800.s1748_sum_of_unique_elements.Solution Maven / Gradle / Ivy

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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy