JAVA Write a method that removes the duplicate elements from

JAVA

Write a method that removes the duplicate elements from an array list of integers using the following header:

public static void removeDuplicate(ArrayList list)

Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space.

Here is a sample run:

Enter ten integers: 10 20 30 20 20 30 50 60 100 9

The distinct integers are: [10, 20, 30, 50, 60, 100, 9]

Solution

public static void removeDuplicate(ArrayList list)
        {     
       
for(int i=0;i<list.size();i++){
  
        for(int j=i+1;j<list.size();j++){
         
            if(list.get(i).equals(list.get(j))){ // to check if duplicate elements
                list.remove(j);                    // remove duplicate
                j--;
            }
      
         
        }
       }
  
System.out.println(list);
        }

JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site