Java Program to Remove duplicate elements from ArrayList

Program to Remove duplicate elements from ArrayList

What is an Array List in Java.

The Java Collection framework includes a dynamic data structure called an ArrayList.The elements are stored in a dynamic array using the Java ArrayList class.

Similar to an array, but with no size restrictions. At any time, we can add or remove components.You can store a group of elements in a resizable array since it implements the List interface.

To know more about Java program to Remove duplicate elements from Array list read the complete article.

Java Set Interface

The Java Collections Framework’s java.util.Set interface extends the java.util.Collection interface, which prohibits duplicate elements and represents a collection of distinct components.

The Set interface offers ways to traverse a set’s elements, as well as ways to add, remove, and check for the presence of components.

  • To remove duplicate elements from an ArrayList, you can convert the ArrayList to a Set, since sets do not allow duplicate elements. You can then convert the Set back to an ArrayList if necessary.

Let’s look at a Java program to Remove duplicate elements from Array list is used to perform an operation.

Example: Java Program to Remove duplicate elements from ArrayList using Set

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

public class Main {
  public static void main(String[] args) {
    List list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(1);
    list.add(2);

System.out.println("Original List: " + list);
    Set set = new HashSet<>(list);
    list.clear();
    list.addAll(set);
    System.out.println("List after removing duplicates: " + list);
  }
}

Output

Original List: [1, 2, 3, 1, 2]
List after removing duplicates: [1, 2, 3]

Example 2 : Encapsulate the operations into functions exhibiting object oriented Programming.

Run
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
   static void remove_duplicates(ArrayList input_list){
      Set temp_set = new LinkedHashSet<>();
      temp_set.addAll(input_list);
      input_list.clear();
      input_list.addAll(temp_set);
      System.out.println("\nThe list with no duplicates is: \n" + input_list);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList input_list = new ArrayList<>(Arrays.asList(100, 200, 225, 275, 350, 475, 500, 650, 900));
      System.out.println("The list is defined as: " + input_list);
      remove_duplicates(input_list);
   }
}

Output

The required packages have been imported
The list is defined as: [100, 200, 225, 275, 350, 475, 500, 650, 900]

The list with no duplicates is: 
[100, 200, 225, 275, 350, 475, 500, 650, 900]

Example 3: Java Program to Remove duplicate elements from ArrayList using Stream

Run
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    List numbers = new ArrayList<>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    numbers.add(1);
    numbers.add(2);
    
    List distinctNumbers = numbers.stream()
        .distinct()
        .collect(Collectors.toList());
    
    System.out.println("Original List: " + numbers);
    System.out.println("Distinct List: " + distinctNumbers);
  }
} 

Output

Original List: [1, 2, 3, 1, 2]
Distinct List: [1, 2, 3]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription