WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 31 of 100

How do you remove duplicates from a List in Java?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

To remove duplicates from a List in Java, there are several approaches you can take depending on your requirements. Here are three common approaches:

Using a Set: One approach is to use a Set to store the unique elements from the List. A Set does not allow duplicates, so adding the elements of the List to a Set will automatically remove any duplicates. You can then create a new List from the Set to preserve the order of the original List.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Example {
    public static void main(String[] args) {
        List<String> listWithDuplicates = new ArrayList<>();
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
        listWithDuplicates.add("c");
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
    
        Set<String> setWithoutDuplicates = new HashSet<>(listWithDuplicates);
        List<String> listWithoutDuplicates = new ArrayList<>(setWithoutDuplicates);
    
        System.out.println("List with duplicates: " + listWithDuplicates);
        System.out.println("List without duplicates: " + listWithoutDuplicates);
    }
}

In this example, we create a List with some duplicate elements and then use a HashSet to remove the duplicates. We then create a new List from the HashSet to preserve the order of the original List.

Using Java 8 Stream API: Another approach is to use Java 8 Stream API’s distinct() method to remove duplicates from the List.

import java.util.ArrayList;
import java.util.List;

public class Example {
    public static void main(String[] args) {
        List<String> listWithDuplicates = new ArrayList<>();
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
        listWithDuplicates.add("c");
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
    
        List<String> listWithoutDuplicates = listWithDuplicates.stream()
                .distinct()
                .collect(Collectors.toList());
    
        System.out.println("List with duplicates: " + listWithDuplicates);
        System.out.println("List without duplicates: " + listWithoutDuplicates);
    }
}

In this example, we create a List with some duplicate elements and then use the distinct() method to remove duplicates from the List. We then collect the distinct elements into a new List.

Using a for loop: A third approach is to use a for loop to iterate over the List and remove any duplicates.

import java.util.ArrayList;
import java.util.List;

public class Example {
    public static void main(String[] args) {
        List<String> listWithDuplicates = new ArrayList<>();
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
        listWithDuplicates.add("c");
        listWithDuplicates.add("a");
        listWithDuplicates.add("b");
    
        List<String> listWithoutDuplicates = new ArrayList<>();
        for (String element : listWithDuplicates) {
            if (!listWithoutDuplicates.contains(element)) {
                listWithoutDuplicates.add(element);
            }
        }
    
        System.out.println("List with duplicates: " + listWithDuplicates);
        System.out.println("List without duplicates: " + listWithoutDuplicates);
    }
}

In this example, we create a List with some duplicate elements and then use a for loop to iterate over the List and add each unique element to a new List.

In summary, there are several approaches to remove duplicates

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics