# Compare the elements of an array against itself

4

I need to go through an arraylist and compare each element of the array with all the elements of the same array and know which one is repeated more times; I have something like that

``````  for (int x = 0; x < lista1.size(); x++) {

int temp = lista1.get(x);

for (int y = 0; y < lista1.size(); y++) {
if (temp == lista1.get(y)) {

System.out.println("temp"+temp);
System.out.println(lista1.get(y));
rep = rep + 1;
rep = 0;
}

}
``````

The problem is that it makes me one for each and it does not compare every x with everything and. And if you have a better idea of how to do it, I'd appreciate it.

asked by Luis David Jimenez 01.07.2017 в 19:36
source

2

You will have problems when the elements are repeated in the list, you will have double the total of repetitions.

To use an iterative method, you could have two variables, `repe` which will be the total number of repetitions of a number in the list, and k as index for the auxiliary list.

We iterate over the list, take the element and iterate in the second for where the condition apart from the typical `i< n` also compares that the auxiliary list does not contain `!contains` the element, because if it contains it already it means that it is a item that has already been evaluated. we avoid the iteration

At the end of the internal for we compare that the number of repetitions is greater than 0 and if so we add the element to the auxiliary list, we increase the index `k` , we carry out the printing and then we reset the counter to 0

``````List<Integer> lista = new ArrayList<>();
int repe = 0;
int k = 0;
List<Integer> repetidos = new ArrayList<>();
for (int x = 0; x < lista.size(); x++) {
for (int y = 0; y < lista.size() && !repetidos.contains(lista.get(x)); y++) {
if (lista.get(x).equals(lista.get(y)))
repe +=1;
}
if(repe>0){
System.out.println("EL ELEMENTO " + repetidos.get(k) + " SE REPITE "+ repe);
k++;
}
repe=0;
}
``````

One way would be to use `Stream` , using the collect to perform a reduction of the initial% `lista` , will reduce it basically to a Key grouping , value that will be assigned to `Map` , where the key will be the word and the value the total of repetitions, for this the method is used counting from the class Collectors

``````List<Integer> lista = new ArrayList<>();
Map<Integer, Long> repeticiones = lista.stream().
collect(Collectors.groupingBy(w -> w, Collectors.counting()));
for (Map.Entry<Integer, Long> entry : repeticiones.entrySet())
System.out.println("Número : " + entry.getKey()+
" , Se Repite : " + entry.getValue()+ " Veces");
``````

source
1

Your code has several logic problems.

I do not understand why you say that you do not compare everything x with everything and, because it is what you are doing.

However, when you compare everything back, any repetition will duplicate you.

Notice that for this array: {1,1,2,2}

You will compare:

1 with 1 ok (against itself) 1 with 1 ok (the second 1) 1 with 2 no 1 with 2 no

Then it goes to the second 1 1 with 1 ok 1 with 1 ok (against itself) 1 with 2 no 1 with 2 no

To do this kind of thing, always compare yourself to forward. That is, from the position in which you are, you always compare from x + 1, then you avoid comparing yourself, and you also avoid comparing what you have already compared.

Besides, you should not compare an item that you have already compared .. Once you reviewed the 1, you should not revise it anymore.

And to know which one is repeated more times, all you have to do is have a variable with the number of repetitions, and then compare with the number of repetitions you found for this case ...

1

With this method you can count how many times each number is repeated.

``````      public void repetidos( ArrayList<Integer> listaAux){
ArrayList<Integer> listaAux=(ArrayList<Integer>)lista.clone();
while (listaAux.size()>0) {
int numVeces=0;
int val=listaAux.get(0);
for (int y = 0; y < listaAux.size(); y++) {
if(val==listaAux.get(y)){
numVeces++;
listaAux.remove(y);
y--;
}
}
System.out.println("El número: "+val+" se repite: "+numVeces);
}
}
``````

## Test Method:

``````  public void prueba(){
ArrayList<Integer> lista1=new ArrayList<Integer>();
repetidos(lista1);
}
``````

## Exit:

The number: 1 is repeated: 4

The number: 2 is repeated: 2

The number: 3 is repeated: 1

0

I have made a function to get the total of repetitions in a list that can serve you. In the following example I create a for loop to go through the list "list1", and check with the function getRepetitions the times that number is repeated and I get it out per terminal.

``````public static void main(String[] args) {
List<Integer> lista1 = new ArrayList<Integer>();
List<Integer> lista3 = new ArrayList<Integer>();

for (int y = 0; y < lista1.size(); y++) {
System.out.println(getRepeticiones(lista1.get(y), lista1));
}

}
static public int getRepeticiones(int valor,List<Integer> lista){
int out = 0;
for(int i=0;i<lista.size();i++){
if(lista.get(i)==valor){
out++;
}
}
return out;
}
``````