My task is to take an ArrayList<SomeType>, and check for duplicate elements. If there is a duplicate element found && it has the same someClassProperty as the first element, use the duplicate element as a parameter in a function call. In the end, remove the duplicates from the original List and the function returns the number of duplicates. (Sorry if my explanation is poor, please look at my code then it's easy to understand)
The problem here is that the code I've come up with is very inefficient and slow, I can't figure out how to make it faster.
public int removeDuplicateElements(){
List<SomeType> duplicates = new ArrayList<SomeType>();
for (int i = 0; i < ListWithDuplicates.size(); i++) {
SomeType firstElement = ListWithDuplicates.get(i);
for (int j = i + 1; j < ListWithDuplicates.size(); j++) {
SomeType otherElement = ListWithDuplicates.get(j);
assert firstElement != otherElement;
if (firstElement.sameProperty(otherElement.getProperty())) {
duplicates.add(otherElement);
firstElement.someFunction(otherElement);
}
}
}
for (SomeType duplicate : duplicates) {
ListWithDuplicates.remove(duplicate);
}
return duplicates.size();
}