I’d like some clarification on what the size– specifically does for this, because the code did not run as expected until I added that. I thought the for loop accounted for change in size with each iteration?

 public static ArrayList<String> removeDuplicates(ArrayList<String> members)
    {
        int size = members.size();
        for(int i = 0; i < size - 1; i++)
        {
            for(int j = i + 1; j < size; j++)
            {
            if(members.get(i).equals(members.get(j)))
            {
             members.remove(i);
             i--;
             size--;
            }
            }
        }
        return members;

I understand what the rest of the code does for the method to account for the changes that occur in an ArrayList when something is removed, but i’m still confused on what that size– does.

New contributor

P M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

members.remove(i); removes the element at i from members. Thus the size of members has changed (and recall you started with int size = members.size(); so size is literally meant to represent members.size()). This method could be simplified with a Set, like

public static List<String> removeDuplicates(ArrayList<String> members) {
    Set<String> set = new LinkedHashSet<>();
    for (String s : members) {
        set.add(s);
    }
    return new ArrayList<>(set);
}

1