0

Below is a simple for loop I am using to try and go through and find the repeated ID's in a array list. The problem is that it only checks one index to the right so quite clearly if there is the same ID two, three or even four indexes across it will miss it and not report it as a repeated ID.

Obviously the goal of this code is to move through each index of the array list, get the ID and check if there are any other identical ID's.

Note for the below arraylist is...arraylist, the getId method simply returns the user ID for that array object.

for (int i=0; i<arraylist.size()-1; i++) {
    if (arraylist.get(i).getId() == arraylist.get(i+1).getId()) {
        System.out.println(arraylist.get(i).getId());
    }
}

What I've tried and keep coming back to is to use two embedded for loops, one for iterating through the array list and one for iterating through an array with userIDs. What I planned on doing is checking if the current arraylist ID was the same as the array with 'pure' IDs and if it wasn't I would add it to the array of 'pure IDs. It would look something like this in psudocode.

for i<-0 i<arraylist size-1 i++ 
for j<-0 j<pureArray size j++ 
    if arraylist.getId(i) != pureArray[j] then
        increment pureArray size by one
        add arraylist.getId(i) to pureArray

In practice perhaps due to my poor coding, this did not work.

So any opinions on how I can iterate completely through my arraylist then check and return if any the gotten IDs have multiple entries.

Thank you.

2 Answers2

0

Looking at leifg's answer on this similar question, you can use two sets, one for duplicates and one for everything else, and you can Set#add(E), which "returns true if this set did not already contain the specified element," to determine whether or not the element is a duplicate. All you have to do is change the sets generics and what you are adding to them:

public Set<Integer> findDuplicates(List<MyObject> listContainingDuplicates)
{ 
  // Assuming your ID is of type int
  final Set<Integer> setToReturn = new HashSet(); 
  final Set<Integer> set1 = new HashSet();

  for (MyObject object : listContainingDuplicates)
  {
   if (!set1.add(object.getID()))
   {
    setToReturn.add(object.getID());
   }
  }
  return setToReturn;
}
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
0

For the purpose of getting duplicates, nested for loop should do the job, see the code below. One more thing is what would you expect this nested for loop to do.

Regarding your pseudocode:

for i<-0 i<arraylist size i++ 
for j<-i+1 j<arraylist size j++ 
    if arraylist.getId(i) != arraylist.getId(j) then
        add arraylist.getId(i) to pureArray

1) Regarding j<- i+1, with every iteration you do not want to compare the same thing many times. With this set up you can make sure you compare first with others, then move to second and compare it to the rest (not including first because you already did this comparison) etc.

2) Incrementing your array every single iteration is highly impractical as you will need to remap and create a new array every single iteration. I would rather make sure array is big enough initially or use other data structure like another ArrayList or just string.

Here is a small demo of what I did, just a quick test, far no perfect.

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        // create a test array with ID strings
        ArrayList test = new ArrayList<>();
        test.add("123");
        test.add("234");
        test.add("123");
        test.add("123");
        String duplicates = "";
        for(int i = 0; i < test.size(); i++) {
            for(int j = i+1; j < test.size(); j++) {
             // if values are equal AND current value is not already a part 
             // of duplicates string, then add it to duplicates string
                if(test.get(i).equals(test.get(j)) && !duplicates.contains(test.get(j).toString())) {
                    duplicates += " " + test.get(j);
                }
            }
        }
        System.out.println(duplicates);
    }

}

Purely for the purpose of finding duplicates, you can also create a HashSet and iteratively add the objects(ID's in your case)to the HashSet using .add( e) method.

Trick with HashSet is that it does not allow duplicate values and .add( e) method will return false if the same value is passed.

But be careful of what values(objects) you are giving to the .add() method, since it uses .equal() to compare whatever you're feeding it. It works if you pass Strings as a value.

But if you're giving it an Object make sure you override .equals() method in that object's class definition (because that's what .add() method will use to compare the objects)

Kaz
  • 1
  • 1