+ 2
What do y'all think about it? Can you find a better way?
static public void duplicateInt(int arr[]) { if(arr.length>1) { HashSet <Integer> red=new HashSet<Integer>(); HashSet <Integer> red2=new HashSet<Integer>(); for(int r:arr) { if (red.add(r)==false) { red2.add(r); } } System.out.println(red2.toString().replace("[", "").replace("]", "")); } else {System.out.println(0);} }
7 Respuestas
+ 3
Denise Roßberg thanks you're right i didn't think about that
+ 2
Hi David
The method find the duplicates in an array?
if (arr.length > 1)
An array which contains only one value can't have any dups.
And I think you could solve it with one hashset.
+ 2
Denise Roßberg but how to solve it with only one Hashset since the second Hashset will help us to print only one element for each repeated element for example 1,4,1,4=1,4
+ 2
Denise Roßberg alright
+ 2
Denise Roßberg looks dope😌🤝
+ 1
David Bukedi
I need to think about it ;)
if (red.contains (r){
System.out.println (r);
} else {
red.add (r);
}
But it works only if every number is at most twice in the array.
Maybe I have another idea.
+ 1
David Bukedi
I changed the HashSet to HashMap ;)
https://code.sololearn.com/c4JBLZ8BxB68/?ref=app