Java logical interview question | Sum a list but ignore any duplicates codewars java solution

realNameHidden - Sep 15 - - Dev Community

Question Link :

https://www.codewars.com/kata/5993fb6c4f5d9f770c0000f2/train/java

import java.util.*;
public class Solution{
    public static int sumNoDuplicates(int[] arr){
      HashMap<Integer,Integer> hm = new HashMap<>();
      for(int i=0;i<arr.length;i++){
        hm.put(arr[i],hm.getOrDefault(arr[i],0)+1);
      }
      int sum = 0;
      for(Map.Entry<Integer,Integer> e : hm.entrySet()){
        if(e.getValue() == 1){
          sum = sum + e.getKey();
        }
      }
      return sum;
    }
}

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player