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

10th Apr 2020, 6:47 PM
David Bukedi Diela
David Bukedi Diela - avatar
7 Réponses
+ 3
Denise Roßberg thanks you're right i didn't think about that
10th Apr 2020, 7:55 PM
David Bukedi Diela
David Bukedi Diela - avatar
+ 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.
10th Apr 2020, 7:32 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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
10th Apr 2020, 7:58 PM
David Bukedi Diela
David Bukedi Diela - avatar
+ 2
10th Apr 2020, 8:05 PM
David Bukedi Diela
David Bukedi Diela - avatar
+ 2
Denise Roßberg looks dope😌🤝
11th Apr 2020, 7:06 AM
David Bukedi Diela
David Bukedi Diela - avatar
+ 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.
10th Apr 2020, 8:03 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
10th Apr 2020, 8:40 PM
Denise Roßberg
Denise Roßberg - avatar