Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to remove duplcates elements from arraylist

// Java program to remove duplicates from ArrayList
  
import java.util.*;
  
public class GFG {
  
    // Function to remove duplicates from an ArrayList
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
    {
  
        // Create a new ArrayList
        ArrayList<T> newList = new ArrayList<T>();
  
        // Traverse through the first list
        for (T element : list) {
  
            // If this element is not present in newList
            // then add it
            if (!newList.contains(element)) {
  
                newList.add(element);
            }
        }
  
        // return the new list
        return newList;
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Get the ArrayList with duplicate values
        ArrayList<Integer>
            list = new ArrayList<>(
                Arrays
                    .asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5));
  
        // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
  
        // Remove duplicates
        ArrayList<Integer>
            newList = removeDuplicates(list);
  
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: "
                           + newList);
    }
}
Comment

Program to remove duplicates in an ArrayList

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

public class ArrayDuplicate {
    public static void main(String args[]) {

List<Integer> num = new ArrayList<Integer>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.add(6);
num.add(3);
num.add(4);
num.add(5);
num.add(6);

System.out.println("Your list of elements in ArrayList : " + num);
Set<Integer> primesWithoutDuplicates = new LinkedHashSet<Integer>(num);
num.clear();
num.addAll(primesWithoutDuplicates);
System.out.println("list of original numbers without duplication: " + num);

}
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: constant arguments in c++ 
Typescript :: Display current directory contents. Long format with user and group IDs displayed numerically And hidden files (starting with .) 
Typescript :: ts(7053) 
Typescript :: typescript blob to base64 
Typescript :: must_not exists elastic search 
Typescript :: adding elements in a specified column or row in a two dimensional array java 
Typescript :: swalert 2 show loader 
Typescript :: typescript key options from array values 
Typescript :: get last item from array ts 
Typescript :: typescript array of object findindex 
Typescript :: typescript valueof interface 
Typescript :: mongoose typescript npm 
Typescript :: add 1 to all elements in array python 
Typescript :: class validator enum 
Typescript :: mysqli_fetch_array() expects parameter 1 to be mysqli_result 
Typescript :: vsc typescript auto build on save 
Typescript :: copy text from file to another file in javascript with fs 
Typescript :: arguments in rust 
Typescript :: class-validator validate nested object 
Typescript :: install lets encrpty 
Typescript :: typescript comments 
Typescript :: reddit requests 429 
Typescript :: java write arraylist of objects to file 
Typescript :: how ro execute typescript file 
Typescript :: how to declare variable in typescript 
Typescript :: number of elements in list in python 
Typescript :: declare object array in typescript 
Typescript :: typescript export async function 
Typescript :: python sort list according to two elements in tuple 
Typescript :: typescript generic object 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =