C++: Different values

Find the different values in a list.

What to build

A program that lets a set remove the repeats.

Requirements

  • Read the numbers.
  • Print N different, and then the different values in increasing order, separated by single spaces.

Input

The first line has n. The second line has n whole numbers.

Output

Two lines.

Constraints

n is from 1 to 1000.

Example

Input:

5
3 1 3 2 1

Output:

3 different
1 2 3

Why it works

A set keeps one copy of each value and stores them in order, so both requirements are met simply by inserting.

Examples

Example 1 · Input
5
3 1 3 2 1
Expected output
3 different
1 2 3
Example 2 · Input
1
9
Expected output
1 different
9

Hints

Hint 1

Insert each number into a set.

Hint 2

size() gives the answer, and a loop gives them in order.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
#include <set>
using namespace std;

int main() {
    int n;
    cin >> n;
    set<int> seen;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        seen.insert(x);
    }
    cout << seen.size() << " different" << endl;
    for (int x : seen) {
        cout << x << " ";
    }
    cout << endl;
    return 0;
}

Lesson: Maps and sets →