1

I am trying to print duplicate elements in one d array using for each loop. But my output was an unexpected one. Could anyone please assist?

package Login;
public class DupsArray {
 static int[] a = {1,2,3,3};
 public static void main(String[] args) {
 int length = a.length;
 for(int i=0;i<=length-1;i++) {
 for(int j : a) {
 for(j=1;j<=length-1;j++) {
 if(a[i]==(a[j]) ) {
 System.out.println("Found duplicate");
 } else {
 System.out.println("No duplicates found");
 }
 }
 }
 }
}

The results show as follows:

The expected results to be print duplicate found.

Bentaye
9,7965 gold badges34 silver badges47 bronze badges
asked Jun 3, 2019 at 7:13

4 Answers 4

1

Try using the below logic which compares every element with all other element in the array, if any duplicate is found,it stops the execution to continue futher

for(int i = 0; i < a.length;i++) {
 for (int j = i + 1 ; j < a.length; j++) {
 if (a[i] == a[j]) {
 System.out.println("Found duplicate");
 return;
 }
 }
}
System.out.println("No duplicate Found");
answered Jun 3, 2019 at 7:21
3
  • This will only print the first duplicate item Commented Jun 3, 2019 at 7:24
  • yes. But in case we need to find all the duplicate rather than returning we can do our desired operation there. Commented Jun 3, 2019 at 7:32
  • Traversing the array 2 timesis bad if we need to find duplicates in big arrays. This will take a lot of time to complete for bigger arrays. Commented Jun 3, 2019 at 11:33
1

We can do something like this

Integer[] arr = {1, 2, 3, 3, 5, 5, 7, 8, 7};
Set<Integer> set = new HashSet<Integer>();
for (Integer i : arr) { 
 if (set.add(i) == false) 
 {
 System.out.println(i);
 } 
}
answered Jun 3, 2019 at 7:45
2
  • yes true, but I got confused why you used 2 different hash sets. Commented Jun 3, 2019 at 7:48
  • In the second set i keep the duplicates for later usage(if needed) Commented Jun 3, 2019 at 7:49
0

try this and update as per your requirement

public class Main{
 public static void main(String[] args) {
 int[] ar = new int[] {1, 1, 3, 3, 4, 5, 7, 8};
 int size = ar.length;
 int error = 0;
 for(int i = 0; i < size; i++){
 for(int j = i+1; j < size; j++){
 if(ar[i] == ar[j]){
 if(i != j){
 error = error + 1;
 System.out.println("dulicate element " + j);
 }
 }
 }
 }
 System.out.println("total number of duplicate element " + error);
 }
}
Thanos M
6737 silver badges27 bronze badges
answered Jun 3, 2019 at 8:03
0
0

You can use Sets like that :

 Integer[] a = {1, 2, 3, 3, 5, 5, 7, 8, 7};
 Set<Integer> duplicatesSet = new HashSet<>();
 Set<Integer> helperSet = new HashSet<>();
 for (Integer element : a) {
 if (!helperSet.add(element)) { // NOTE*
 System.out.println("Duplicate found : " + element);
 duplicatesSet.add(element);
 } 
 }

Then you can do whatever you like with the duplicates set

for(Integer el : duplicatesSet){
 System.out.println(el);
}

Note*

According to javadocs :

boolean add(E e);

Adds the specified element to this set if it is not already present

return true if this set did not already contain the specified element

This way we can know if the element is already in the set, if it is we can add it to our duplicates.

answered Jun 3, 2019 at 7:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.