PHP: Remove repeats

Keep each value only once, in the order it first appeared.

What to build

A program that removes repeated numbers from a list.

Requirements

  • Read n, then n integers.
  • Print the numbers without repeats, in order of first appearance, separated by spaces.

Input

Line 1: n. Line 2: n integers.

Output

One line.

Constraints

1 ≤ n ≤ 100.

Example

Input:

6
4 2 4 3 2 9

Output:

4 2 3 9

Why it works

Each value is kept the first time it is seen and skipped afterwards, so the order is not changed.

Examples

Example 1 · Input
6
4 2 4 3 2 9
Expected output
4 2 3 9
Example 2 · Input
3
1 1 1
Expected output
1

Hints

Hint 1

array_unique keeps the first copy of each value, in the original order.

Hint 2

Or use in_array inside a loop to decide whether to add a value.

Solution

Show one correct solution (try it yourself first)
<?php
$n = (int) trim(fgets(STDIN));
$a = array_map('intval', explode(" ", trim(fgets(STDIN))));
$seen = [];
foreach ($a as $x) {
    if (!in_array($x, $seen)) {
        $seen[] = $x;
    }
}
echo implode(" ", $seen), "\n";

▶ Open this solution in the compiler

Lesson: Arrays →