Q:

(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)

0

(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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (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)             *
*********************************************************************************/
import java.util.ArrayList;

public class Exercise_19_03 {
	/** Removes duplicate elements from an array list */
	public static <E extends Comparable<E>> 
			ArrayList<E> removeDuplicates(ArrayList<E> list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = i + 1; j < list.size(); j++) {
				if (list.get(i).compareTo(list.get(j)) == 0)
					list.remove(j);
			}
		}
		return list;
	} 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now