Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Invert sign and add
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { int[] arr={-3,5,6,4,-3,5}; int sum=0,count; HashSet<Integer> hash=new HashSet<>(); for(int i:arr){ count=0; if(!hash.contains(i)){ hash.add(i); count=getCount(i,arr); if(count>1){ sum+=(i*-1)*count; }else{ sum+=i; } } } System.out.println(sum); } public static int getCount(int n,int[] arr){ int res=0; for(int i:arr){ if(i==n){ res++; } } return res; } }
run
|
edit
|
history
|
help
0
Sumod
Check Case
4th ques
Java Class and Object
1
MAP List Iterator
switching light bulbs
Quadratic equation
pass
Game developer