Print each word of a line once, and say how many different words there were.
What to build
A program that lets a Set do the work of finding repeats.
Requirements
- Read a line of words.
- Print the different words, in the order each first appears, separated by single spaces.
- Print
N differenton the next line. - Words that differ in capital letters count as different.
Input
One line of words separated by single spaces.
Output
Two lines.
Constraints
There are from 1 to 200 words.
Example
Input:
a b a c b
Output:
a b c
3 different
Why it works
A Set ignores a value it already holds and remembers the order of first appearance, so both requirements are met by creating it.
Examples
Example 1 · Input
a b a c b
Expected output
a b c 3 different
Example 2 · Input
x
Expected output
x 1 different
Hints
Hint 1
new Set(words) keeps one copy of each word.
Hint 2
[...set] turns it back into an array, in first-seen order.
Hint 3
set.size is the number of different words.
Solution
Show one correct solution (try it yourself first)
const unique = new Set(prompt().split(" "));
console.log([...unique].join(" "));
console.log(`${unique.size} different`);