Deduplicate article tags
An editor pasted a comma-separated tag list with repeats. Print every tag once, preserving the order in which it first appeared, and join the cleaned tags with | .
Input
One line of 1 to 30 lowercase tags separated by commas.
Output
One line: unique tags separated by | .
Example 1
Input
sql,data,joins,data,practice
Output
sql | data | joins | practice
The second data tag is removed and all first occurrences keep their positions.
Constraints
- Each tag has 1 to 20 lowercase letters
- At least one tag is present
Hints
Hint 1 of 3
split(',') creates the input array.
Hint 2 of 3
includes tells you whether the result already has a tag.
Hint 3 of 3
push only unseen tags, then join the result.
Solution
Show a reference solution and explanation
const tags = readline().split(',');
const unique = [];
for (const tag of tags) {
if (!unique.includes(tag)) unique.push(tag);
}
console.log(unique.join(' | '));
Why it works
The left-to-right scan preserves the first occurrence naturally. A Set could also deduplicate, but the explicit array loop makes the membership decision and output order easy to follow.
Lesson for this exercise: JavaScript Arrays: Creating, Indexing and Changing Lists