0

I have a list of list:

List<List<Integer>> myList = new ArrayList<>();

What would be the best way to remove the duplicated list in myList?

For example, in the following list of list:

[[-1,0,1],[-1,-1,2],[-1,0,1]]

I would like to reduce it to:

[[-1,0,1],[-1,-1,2]]

Thanks!

Edamame
  • 20,574
  • 59
  • 165
  • 291

2 Answers2

1

The easiest way is to copy it into an order-preserving set (or, more generally, any kind of set, if you don't care about the ordering), and then back into the list:

myList = new ArrayList<>(new LinkedHashSet<>(myList));
Andy Turner
  • 131,952
  • 11
  • 151
  • 228
0

Use a Set instead of a list. Sets do not allow duplicates. What is the difference between Set and List?

Tom O.
  • 5,449
  • 2
  • 20
  • 35