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 this
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;
lista3.add(temp);
lista3.add(rep);
rep = 0;
}
}
The problem is that it makes me a for of each one and does not compare each x with all y . And if you have a better idea of how to do it, I would appreciate it.
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 an index for the auxiliary list.We iterate over the list, we take the element and we iterate in the second for where the condition, apart from the typical one,
i< n
also checks that the auxiliary list does not contain!contains
the element, because if it already contains it, it means that it is an element that has already been evaluated. we avoid iterationAt 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 increment the index
k
, we print and then we reset the counter to 0One way would be to use , through the collect
Stream
method to perform a reduction of the initial , it will basically reduce it to a Key Grouping , value that will be assigned to , where the key will be the word and the value the total repetitions , for this it is used the counting method of the Collectors classlista
Map
Your code has several logic problems.
I do not understand why you say that it does not compare all x with all y, because that is what it is doing.
However, when you compare everything back, any reps are going to be doubled for 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 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 forward. In other words, from the position you are in, you always compare from x+1, so you avoid comparing with itself, and you also avoid comparing what you have already compared.
Also, you shouldn't compare an item you already compared. Once you've checked 1, you shouldn't check it any more.
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 that you found for this case...
With this method you can count how many times each number is repeated.
Testing method:
Departure:
I have made a function to get the total repeats in a list that may be of use to you. In the following example I create a for loop to go through the list "list1", and I check with the getRepetitions function how many times that number is repeated and I output it through the terminal.