list without duplicates java code example

Example 1: java remove equal element of a list

public static <T> List<T> removeDuplicates(List<T> list){
  Set<T> set = new LinkedHashSet<>(list);
  return new ArrayList<T>(set);
}

Example 2: 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 3: compare two lists and remove duplicates java

listA.removeAll(new HashSet(listB));

Example 4: remove duplicates from list java

ArrayList<Object> withDuplicateValues;
 HashSet<Object> withUniqueValue = new HashSet<>(withDuplicateValues);
 
 withDuplicateValues.clear();
 withDuplicateValues.addAll(withUniqueValue);