Numbers Both Lists Share
You are given two lists of integers, first and second. Return every number that appears in both lists, each number once, in the order of its first appearance in first. If the lists share no number, return an empty list [].
Examples
first = [4, 9, 5], second = [9, 4, 9, 8, 4] → [4, 9]
first = [1, 2, 2, 1], second = [2, 2] → [2]
first = [1, 3, 5], second = [2, 4, 6] → []
first = [7, -1, 7, 0], second = [0, 7] → [7, 0]In the last example 7 comes before 0 in first, so it comes first in the answer, and only once.
Constraints
- Each list holds between 0 and 10,000 numbers, each between -1,000,000,000 and 1,000,000,000.
- Aim for O(n + m): one look at each number of each list.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.