/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.program;

import java.util.*;
/**
* Distinct elements in ArrayList.
* Write the following method that returns a new ArrayList.
* The new list contains the non-duplicate elements from the original list.
*
* public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list)
*
*/
public class TestProgram {
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list)
{
//making empty ArrayList
ArrayList<E> out=new ArrayList<E>();
//frame into list
for(int i=0;i<list.size();i++)
{
//confirm alreay present in ArrayList or not
if(!out.contains(list.get(i)))
{
// if not present then add it
out.add(list.get(i));
}
}
// then return list
return out;
}

public static void main(String[] args) {
//prepare an object
Random r=new Random();
ArrayList<Integer> arr=new ArrayList<Integer>();
System.out.println(“Input ArrayList:”);
//have a loop
for(int i=0;i<50;i++)
{
//create and produce number in ArrayList
arr.add(r.nextInt(20)+1);
System.out.print(arr.get(i)+” “);
}
//call function and store output
ArrayList<Integer> out=removeDuplicates(arr);
//produce output
System.out.println(“nOutput ArrayList:”);
for(int i=0;i<out.size();i++)
{
System.out.print(out.get(i)+” “);
}
}

Subscribe For Latest Updates
Let us notify you each time there is a new assignment, book recommendation, assignment resource, or free essay and updates