Numbers Both Lists Share

EasyHash Map≈ 15 min

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

text
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.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
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.