check for duplicate in Arraylist java code example
Example 1: java array check duplicates
duplicates = false;
for(i = 0; i < zipcodeList.length; i++) {
for(j = i + 1; k < zipcodeList.length; j++) {
if(j != i && zipcodeList[j] == zipcodeList[i]) {
duplicates = true;
}
}
}
Example 2: java find duplicate element in list
public static Set<String> findDuplicates(List<String> listContainingDuplicates) {
final Set<String> setToReturn = new HashSet<String>();
final Set<String> set1 = new HashSet<String>();
for (String yourInt : listContainingDuplicates) {
if (!set1.add(yourInt)) {
setToReturn.add(yourInt);
}
}
return setToReturn;
}