0150

Deduplicate, Keep the Order

text
list
dictionary

Deduplicate, Keep the Order

Your extension builds the CC line for order confirmations by gluing together addresses from several sources — the customer's contact card, the salesperson, a notification setup table. Nobody checks for overlaps, so the same address sneaks in two or three times, sometimes with different casing or stray spaces, and customers get triplicate emails. You are writing the cleaner every send now goes through.

Requirements

Create a codeunit named "Recipient Deduplicator" with one public procedure:

procedure Dedupe(Recipients: Text): Text

Recipients is a list of email addresses separated by semicolons (;). Entries may carry leading or trailing spaces, and the list may contain empty entries — nothing between two semicolons, or only spaces.

The returned text must follow these rules — all of them are graded:

  1. Trim leading and trailing spaces from every entry; entries that are empty after trimming are dropped.
  2. Two entries are duplicates when they are equal ignoring case: Sales@Contoso.com and sales@contoso.com are the same recipient.
  3. Keep only the first occurrence of each recipient, in the order recipients first appear, with that first occurrence's original casing.
  4. Join the kept entries with '; ' — a semicolon followed by exactly one space, with no leading or trailing separator.
  5. If nothing survives — empty input, or only semicolons and spaces — return an empty text ('').

What the tests check

The tests call Dedupe and compare the returned text exactly — note rule 4's semicolon-plus-space. Fixed cases cover a single recipient, exact duplicates, duplicates differing only in casing, entries with surrounding spaces, empty entries, an empty input, and an input of only separators and spaces. One test builds a randomized list where every address reappears later in upper case with padding, so hardcoding the fixed examples fails.

Learn More

Hint 1
The Text type has built-in methods that break a delimited string into pieces and strip surrounding spaces from a piece — no manual position arithmetic with StrPos and CopyStr needed.
Hint 2
Answering 'have I kept this address already?' needs a normalized form: compare the lowercased trimmed entry, but append the original trimmed entry to the output so the first occurrence's casing survives.
Hint 3
Recipients.Split(';') returns a List of [Text] you can foreach over, and Entry.Trim() cleans each piece. Store the lowercased entries you have already kept in a Dictionary of [Text, Boolean] (or a List of [Text]) — one ContainsKey/Contains lookup decides whether to append — and assemble the result with '; ' between appends.
ALBusiness Central 28.5
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.