Java program to remove duplicate elements from arraylist

Here's an example Java program that removes duplicate elements from an ArrayList:

refer to:‮igi‬ftidea.com
import java.util.ArrayList;
import java.util.LinkedHashSet;

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

        System.out.println("Original ArrayList: " + list);

        LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(list);

        ArrayList<Integer> newList = new ArrayList<Integer>(set);

        System.out.println("ArrayList after removing duplicates: " + newList);
    }
}

This program first creates an ArrayList containing some duplicate elements. It then creates a LinkedHashSet and passes the ArrayList as an argument to the constructor. The LinkedHashSet automatically removes duplicates because it only allows unique elements to be stored. The LinkedHashSet is then converted back to an ArrayList to create a new list containing only the unique elements. Finally, the new list is printed to the console.