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
  • 23,718
  • 73
  • 186
  • 320

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
  • 137,514
  • 11
  • 162
  • 243
0

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

Tom O.
  • 5,730
  • 2
  • 21
  • 35