Home > database >  Check for duplicates while populating a string array
Check for duplicates while populating a string array

Time:11-03

I'm having a hard time thinking about how should I implement the checking for duplicates while the string array with length of 5 is initially empty. Before adding an element in the array, I have to check first if it already exists in the array but because the array is initially empty (which means the five elements are null) it prompts an error, I think that is because I'm trying to compare the element (that I'm trying to add in the array) to null.

What I want to do is check if the the length of the array is less than the limit, check if the element that I want to add has no duplicate in the array. If it doesn't have a duplicate, then I'll add it in array, if it has a duplicate then I won't add it then I'll print a prompt message.

I am working on a project with multiple classes, here's the snippet of my code:

public class Collections {
    Guardian[] guardians;
    int count;
    
    final static int MAX_GUARDIANS = 5;

    public Collection () {
        guardians = new Guardian[Collection.MAX_GUARDIANS];
    }

    public void addGuardians (Guardian guardian) {

        if (this.count < MAX_GUARDIANS) {
            for (int i = 0; i < guardians.length; i  ) {
                if (guardians[i].equals(guardian)) {
                    System.out.println("The guardian is already in the list!\n");
                } else {
                    this.guardians[this.count  ] = guardian;
                    System.out.println("Guardian " guardian.getName() " was added to the list!");
                }
            }

        } else {
            System.out.println("Maximum number of guardians in the list has been reached!\n");
        }

    }
}

Is it possible to compare the element that I'm planning to add to null?

CodePudding user response:

So when you want to search for a duplicate, you have to search the whole array first. Then if there's no duplicates, add an element after the for loop.

        for (int i = 0; i < count; i  ) {
            if (guardians[i].equals(guardian)) {
                System.out.println("The guardian is already in the list!\n");
                return;   // <-- add this to EXIT when find a match
            }
         }
         // now that you've searched the whole list, 
         // you can add a new element
         guardians[count  ] = guardian;
         System.out.println("Guardian " guardian.getName() " was added to the list!");

   

CodePudding user response:

You can try using a HashSet<String> to keep track of duplicates, and keep track of unique strings in an array.

Declare a hashset:

HashSet<String> set = new HashSet<String>();
// Or:
// Set<String> set = new HashSet<String>();

Check if a string is in a set with:

if(set.contains("Hello")) {
    // String is in the set
}

Add a string to a set with:

set.add("Hello");

CodePudding user response:

Use a list instead of an array to be able to check if it contains that guardian already.

public class Collections {
    List<Guardian> guardians;
    int count;

    final static int MAX_GUARDIANS = 5;

    public Collections () {
        guardians = new LinkedList<>();
    }

    public void addGuardians (Guardian guardian) {
        if (guardians.size() >= MAX_GUARDIANS) {
            System.out.println("Maximum number of guardians in the list has been reached!\n");
        return;
        }
    
        if (guardians.contains(guardian)) {
            System.out.println("The guardian is already in the list!\n");
        } else {
            guardians.add(guardian);
            System.out.println("Guardian " guardian.getName() " was added to the list!");
        }
    }
}
  • Related