voddan
03/05/2019, 11:47 AMlist.size == list.distinct().size
, which is efficient, but uglyhho
03/05/2019, 12:16 PMfun Collection<*> allDistinct(): Boolean = this.size == this.distinct().size
sitepodmatt
03/05/2019, 12:19 PMfun <T> List<T>.isDistinct() = this.fold(mutableMapOf<T,Int>()) { acc, i ->
acc.also {
it[i] = ((it[i] ?: 0) + 1).also { count ->
if(count > 1) {
return false
}
}
}
}.let { true }
marstran
03/05/2019, 12:57 PMfun <T> List<T>.isDistinct(): Boolean {
val mutSet = mutableSetOf<T>()
for (elem in this) {
if (elem in mutSet) {
return false
}
mutSet.add(elem)
}
return true
}
Pavlo Liapota
03/05/2019, 1:09 PMfun <T> List<T>.isDistinct(): Boolean {
val mutSet = mutableSetOf<T>()
return all { mutSet.add(it) }
}
voddan
03/05/2019, 1:23 PM