Skip to content
BytePatterns

Shared Values Of Two Lists

EasyHash Tables#hash-set#membership-test~15m

Problem

Given two lists of values, return the values that occur in both of them. Each shared value appears once in the result even when either list repeats it, and the result follows the order in which the values first show up in the first list.

Examples

Input:  a = [1, 2, 2, 1], b = [2, 2]
Output: [2]
Why:    a shared value is reported once no matter how often it repeats
Input:  a = [4, 9, 5], b = [9, 4, 9, 8, 4]
Output: [4, 9]
Why:    the order comes from the first list, not the second
Input:  a = [1, 2], b = [3]
Output: []
Why:    edge case, the lists have nothing in common

Hints

0 / 3

Stuck on the idea rather than the code? Hash Table Basics covers it.