Print only matches in an array

1

I have two arrays, one with 20 random numbers and another with those same 20 numbers but WITHOUT the repeating numbers. In addition, the second array is sorted in ascending order. My intention is to print the numbers without repetitions but with the order that the 20 random numbers had. I hope you have explained me well. This is what I have for now. EDIT: The 20 numbers are ordered by keyboard. These 20 numbers are those contained in the first array

int[] entrada = new int[20];

This array I have duplicated to be able to compare it when extracting the repeated numbers in another method (I do not think it is necessary to put the method)

int[] duplicado = new int[20]
for (int i = 0; i<entrada.length; i++){
    duplicado[i] = entrada[i];
}

And this is what I have tried to print only the numbers without repetitions and by the order that the user has entered ::

for (int i = 0; i<duplicado.length; i++){
     int j = 0;
     if(duplicado[i] == entrada[j]){    
          System.out.println(duplicado[i]);
     }else{
          j++;
     }

}
    
asked by h3h3h3h3 28.11.2018 в 07:16
source

2 answers

1

The way you're doing it, you're not traversing the array well. Let me explain:

You start to go through the duplicate array and see that duplicado[0] equals entrada[0] .

If they are not equal, j is j++ so j=1 . In addition, the for keeps going so in the next pass you are looking for duplicado[1] equal to entrada[1] .

And in the next you see that duplicado[2] equals entrada[2] .

This means that you do not compare all with everyone. To get what you want you need to have two for sentences. The code would be like this:

ArrayList<Integer> imprimidos = new ArrayList<Integer>();
for (int i = 0; i<duplicado.length; i++){
     for (int j = 0; j<entrada.length; j++){
         if(duplicado[i] == entrada[j] && !imprimidos.contains(entrada[j])){    
              System.out.println(duplicado[i]);
              imprimidos.add(duplicado[i]);
         }
     }
}

With the two for you are looking at the position duplicado[i] position by position (0,1,2,3,4, etc.) and compare them with all the positions of the array entrada[j] .

For better code, if you only want to find a single match and stop looking for it (since you have found it once) you can add a return in if to search for the next.

ArrayList<Integer> imprimidos = new ArrayList<Integer>();
for (int i = 0; i<duplicado.length; i++){
     for (int j = 0; j<entrada.length; j++){
         if(duplicado[i] == entrada[j]  && !imprimidos.contains(entrada[j])){    
              System.out.println(duplicado[i]);
              imprimidos.add(duplicado[i]);
              return;
         }
     }
}
    
answered by 28.11.2018 / 08:43
source
0

From what I understand you already have the array Duplicated the numbers without duplicates and ordered in ascending order, then what you would have to do to print the non-duplicates in the original order is to go through the input array looking for if the numbers are in the duplicate array, and in case you are printing it and continuing with the execution, but you simply do nothing because it is a duplicate number.

That is, something like this:

    for (int i = 0; i<entrada.length; i++){
        for (int j = 0; j < duplicado.length; j++){
            if (entrada[i] == duplicado[j]){
                System.out.println(duplicado[i]);
                break;
            }
        }
    }
    
answered by 28.11.2018 в 10:18