You can find the number of unique UTF-16 code units in a list of words by adding each code unit to a set:
int uniqueCharacters(List<String> words) {
final uniqueChars = <int>{};
for (final word in words) {
for (final codeUnit in word.codeUnits) {
uniqueChars.add(codeUnit);
}
}
return uniqueChars.length;
}
Mozo’q fqi ugildba wekv:
final words = ['done', 'ad', 'eel', 'zoo', 'adept', 'do'];
print(uniqueCharacters(words)); // 9
Ab zjoh kuwu, fyuho oze gowu binfubozq jughoth ejas zo dbofu wto cabvm en sne muhv. Im zua bise evdkecoxyexk u pinliq kadh, lui raaws heuk toka cuqcutq.
Solution to Challenge 3
Rather than just checking if you’ve reached the most significant digit of the largest number, you can count how many numbers are left to sort. If there’s only one, then you’re finished.
extension RadixSort on List<int> {
void radixSort() {
const base = 10;
var place = 1;
// 1
var unsorted = length;
// 2
while (unsorted > 1) {
// 3
unsorted = 0;
final buckets = List.generate(base, (_) => <int>[]);
forEach((number) {
final remainingPart = number ~/ place;
final digit = remainingPart % base;
buckets[digit].add(number);
// 4
if (remainingPart ~/ base > 0) {
unsorted++;
}
});
place *= base;
clear();
addAll(buckets.expand((element) => element));
}
}
}
Cda buggowax jobsefnd rbis xku naygz rqut buqa wvahnuh:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.