tezvyn:

Deduplicate a List of emails in Dart

AI-drafted, machine-checkedSource: api.dart.devbeginner

Tests knowledge of Dart's Set semantics and LinkedHashSet insertion order. The idiomatic solution is emails.toSet().toList(), which deduplicates in O(n) time while preserving order. Avoid manual loops with contains, which are O(n^2) and unidiomatic.

WHAT THIS TESTS: This question evaluates whether you understand Dart collection semantics beyond basic loops. Specifically it checks if you know that the Set class enforces uniqueness using Object.== and Object.hashCode, that the default concrete implementation is LinkedHashSet which preserves insertion order, and that you can move between List and Set efficiently using the standard library. It also surfaces whether you think about algorithmic complexity since a naive approach can turn a linear problem into a quadratic one.

A GOOD ANSWER COVERS: First state the idiomatic one-liner emails.toSet().toList(). Second explain that toSet creates a Set implementation automatically dropping duplicates because Sets allow each object to occur only once. Third note that Dart's default Set is a LinkedHashSet so iteration order matches insertion order meaning the resulting List keeps the first occurrence of each email in its original position. Fourth mention complexity which is average O(n) time and O(n) space because each element is hashed and inserted once. Optionally mention Set.of(emails).toList() as an equivalent alternative.

COMMON WRONG ANSWERS: A major red flag is manually iterating with a for-loop and calling contains on a secondary List before adding which is O(n^2) and verbose. Another is using distinct() as if it were a standard Dart Iterable method when it actually comes from external packages like RxDart. Some candidates also incorrectly assume that converting to a Set shuffles or loses order revealing a gap in knowledge about LinkedHashSet. Finally suggesting sorting first to group duplicates is over-engineered and destroys the original sequence.

LIKELY FOLLOW-UPS: An interviewer might ask how you would handle case-insensitive deduplication which requires normalizing strings to lowercase before the Set conversion. They might ask about memory trade-offs for very large lists or what happens if email objects override == and hashCode poorly. Another variant is asking for a way to keep only the first unique elements without creating an intermediate Set explicitly though toSet().toList() is still the community standard.

ONE CONCRETE EXAMPLE: Given a list containing alice@example.com, bob@example.com, alice@example.com, and charlie@example.com, calling toSet then toList produces alice@example.com, bob@example.com, and charlie@example.com. The duplicate alice entry is removed and the original first occurrence order is preserved because LinkedHashSet tracks insertion order.

Read the original → api.dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.