Created
June 2, 2022 21:03
-
-
Save leo-from-spb/d08830628a73fd0f11221e0d3923137b to your computer and use it in GitHub Desktop.
How to de-duplicate a list of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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