Skip to content

Instantly share code, notes, and snippets.

@leo-from-spb
Created June 2, 2022 21:03
Show Gist options
  • Save leo-from-spb/d08830628a73fd0f11221e0d3923137b to your computer and use it in GitHub Desktop.
Save leo-from-spb/d08830628a73fd0f11221e0d3923137b to your computer and use it in GitHub Desktop.
How to de-duplicate a list of strings
class X {
private val obst = listOf("Apple", "Pear", "Pear", "PEAR", "APPLE", "APRICOT", "Apple")
@Test
fun deduplicateCaseInsensitively_1() {
val deduplicated = obst.asSequence().sortedWith(String.CASE_INSENSITIVE_ORDER).fold(ArrayList<String>(obst.size)) { a, o ->
if (a.isEmpty() || !a.last().equals(o, true)) a += o
a
}
println(deduplicated)
}
@Test
fun deduplicateCaseInsensitively_2() {
val deduplicated = obst.asSequence().toSortedSet(String.CASE_INSENSITIVE_ORDER)
println(deduplicated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment