What is ArrayList?

Type-1:

 Below is a Java program that removes all duplicate elements from an ArrayList of integers:

import java.util.ArrayList;
import java.util.HashSet; 
import java.util.Scanner; 
public class RemoveDuplicates
public static void main(String[] args)
// Create an ArrayList of integers 
 ArrayList<Integer> numbersList = new ArrayList<>(); 
numbersList.add(5);
numbersList.add(50);
numbersList.add(15);
numbersList.add(25);
numbersList.add(5);
 // Remove duplicates from the ArrayList
removeDuplicates(numbersList); 
// Print the ArrayList after removing duplicates
System.out.println("ArrayList after removing duplicates: " + numbersList);
 } 
// Method to remove duplicates from the ArrayList 
public static void removeDuplicates(ArrayList<Integer> list) {
 HashSet<Integer> set = new HashSet<>(list); 
 list.clear();
 list.addAll(set); 
 } 
}


Example usage:

ArrayList after removing duplicates: [5, 50,15,25]

Explanation:

  1. We start by creating an ArrayList of integers called numbersList.
  2. The user is prompted to input integers to add to the list, and the input loop continues until the user enters -1.
  3. The method removeDuplicates() takes the ArrayList as input and removes duplicates using a HashSet. A HashSet automatically removes duplicate elements while preserving the uniqueness of elements.
  4. We clear the ArrayList and then add the unique elements back to it from the HashSet.
  5. The ArrayList after removing duplicates is printed to the console.

By using a HashSet, we can efficiently remove duplicates from the ArrayList, ensuring that only distinct elements remain in the list.


Type-2:

 Another way to remove duplicates from an ArrayList of integers is by using the List interface's removeIf() method along with a temporary HashSet to keep track of seen elements. 

Here's the updated Java program:


import java.util.ArrayList; 
import java.util.HashSet;
import java.util.List; 
import java.util.Scanner;
public class RemoveDuplicates
public static void main(String[] args) {
// Create an ArrayList of integers
ArrayList<Integer> numbersList = new ArrayList<>();
numbersList.add(5);
numbersList.add(50);
numbersList.add(15);
numbersList.add(25);
numbersList.add(5);
// Remove duplicates from the ArrayList
removeDuplicates(numbersList); 
// Print the ArrayList after removing duplicates
System.out.println("ArrayList after removing duplicates: " + numbersList);
 } 
// Method to remove duplicates from the ArrayList 
public static void removeDuplicates(List<Integer> list)
 HashSet<Integer> set = new HashSet<>(); 
 list.removeIf(e -> !set.add(e)); 
 } 
}


Example usage:

ArrayList after removing duplicates: [5, 50,15,25]

Explanation:

  1. We start by creating an ArrayList of integers called numbersList.
  2. The user is prompted to input integers to add to the list, and the input loop continues until the user enters -1.
  3. The method removeDuplicates() takes the ArrayList as input and removes duplicates using a temporary HashSet set.
  4. The removeIf() method is used to remove elements from the ArrayList that do not satisfy the given condition. In this case, we keep only those elements for which set.add(e) returns true. When adding an element to the HashSet, it returns true if the element was not already present (i.e., it is a new element). Hence, this condition effectively filters out duplicates from the ArrayList.

Using removeIf() along with a temporary HashSet is a concise and efficient way to remove duplicates from an ArrayList in Java.


Type-3:

Another way to remove duplicates from an ArrayList of integers is by using a nested loop to compare elements and manually remove duplicates. Here's the updated Java program:


import java.util.ArrayList; 
import java.util.Scanner; 
public class RemoveDuplicates
public static void main(String[] args)
// Create an ArrayList of integers
ArrayList<Integer> numbersList = new ArrayList<>(); 
numbersList.add(5);
numbersList.add(50);
numbersList.add(15);
numbersList.add(25);
numbersList.add(5);
 // Remove duplicates from the ArrayList
removeDuplicates(numbersList); 
// Print the ArrayList after removing duplicates
System.out.println("ArrayList after removing duplicates: " + numbersList);
 } 
// Method to remove duplicates from the ArrayList 
public static void removeDuplicates(ArrayList<Integer> list) {
int size = list.size();
for (int i = 0; i < size; i++) {
int currentElement = list.get(i); 
for (int j = i + 1; j < size; j++) { 
if (list.get(j) == currentElement) {
 list.remove(j);
 size--; 
 j--; 
// Compensate for the removal 
 } }
 } }
}

      

Example usage:

ArrayList after removing duplicates: [5, 50, 15, 25]


Explanation:

  1. We start by creating an ArrayList of integers called numbersList.
  2. The user is prompted to input integers to add to the list, and the input loop continues until the user enters -1.
  3. The method removeDuplicates() takes the ArrayList as input and removes duplicates using a nested loop.
  4. The outer loop runs through each element in the ArrayList.
  5. The inner loop starts from the element next to the current element (i+1) and compares it with the current element. If it finds a duplicate, it removes that duplicate from the ArrayList.
  6. The size variable is used to keep track of the number of elements in the ArrayList. Since we are removing elements from the list, we decrement the size when a duplicate is removed.

While this approach manually removes duplicates, it is not as efficient as using HashSet or removeIf() with HashSet. The time complexity of this approach is O(n^2), where n is the number of elements in the ArrayList, as each element is compared with all subsequent elements in the inner loop. Using HashSet ensures a more efficient O(n) time complexity for removing duplicates.