Java Program to Remove duplicate elements from ArrayList

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to remove duplicate elements from arrayList. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified.

Below is a demonstration of the same −

Suppose our input is

Input list : [150, 250, 300, 250, 500, 150, 600, 750, 300]

The desired output would be

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]

Algorithm

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 – Create an ArrayList of integer values and initialize elements in it.
Step 5 - Display the ArrayList on the console.
Step 6 - Create another linkedhashset of integers.
Step 7 - Use the ‘addAll’ method to include elements from previous ArrayList into it as elements.
Step 8 - Since it is a set, it only adds the unique values.
Step 9 - Clear the elements of the ArrayList.
Step 10- Display the set on the console with unique elements.
Step 11- Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
      System.out.println("The list is defined as: " + input_list);
      Set<Integer> 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);
   }
}

Output

The required packages have been imported
The list is defined as: [150, 250, 300, 250, 500, 150, 600, 750, 300]

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Demo {
   static void remove_duplicates(ArrayList<Integer> input_list){
      Set<Integer> 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<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
      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: [150, 250, 300, 250, 500, 150, 600, 750, 300]

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]
raja
Updated on 30-Mar-2022 10:34:39

Advertisements