22

How would one remove duplicates from an unordered Immutable.List()? (without using toJS() or toArray())

e.g.

Immutable.List.of("green", "blue","green","black", "blue")
ThorbenA
  • 1,863
  • 3
  • 16
  • 27

2 Answers2

39

You can convert it to a Set. A Set is a List with unique values.

Immutable.List.of("green", "blue","green","black", "blue").toSet()

If you need it as list again just convert it back then:

Immutable.List.of("green", "blue","green","black", "blue").toSet().toList()

Update:

It exists a shorter possibility to get unique values:

Immutable.List.of("green", "blue","green","black", "blue").distinct
Varon
  • 3,736
  • 21
  • 21
30

If you have a more complex type you can also use groupBy to provide your own selector to compare on. The following will remove duplicates on the property .name:

var distinctColors = duplicateColors.groupBy(x => x.name).map(x => x.first()).toList();