-4

Possible Duplicate:
How do I remove repeated elements from ArrayList?

I have one list in that elements like

[10,11,12,10,12,13,10,12,11,11].

The requirement is when ever we enter one duplicate value remove the remaining duplicate values. Ex:if we remove the 10 then automatically remove two tens.

Community
  • 1
  • 1
BOBBY
  • 61
  • 4
  • 11
  • http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist and http://stackoverflow.com/questions/4778260/how-to-remove-arraylist-duplicate-values - possible duplicates. – verisimilitude Sep 03 '12 at 05:28

2 Answers2

1

Try using List.removeAll coupled with Collections.singleton.

final List<Integer> list
    = new LinkedList<Integer>(Arrays.asList(10, 11, 12, 10, 12,
          13, 10, 12, 11, 11));
list.removeAll(Collections.singleton(10));
assert list.equals(Arrays.asList(11, 12, 12, 13, 12, 11, 11));
obataku
  • 29,212
  • 3
  • 44
  • 57
0

You can use removeAll() method of list as:

List<Integer> tempList = new ArrayList<Integer>();
tempList.add(10);
list.removeAll(tempList);

or you can use Collections.singleton() as:

list.removeAll(Collections.singleton(10));
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63