Shared items
Inputs
first and second are lists of 0 to 10 whole numbers, each from 0 to 15.
Your tasks
- Create an empty list
common. - Use an outer loop over
first. For each number, use an inner loop oversecondto see whether it appears there. - Add the number to
commonif it appears insecondand is not already incommon. - Complete
commonItemsso it returnscommon. Return it, do not print it.
Example
With first = [4, 8, 8, 2, 9] and second = [9, 2, 5, 8], commonItems returns [8, 2, 9].
Rules
- Keep the order in which the numbers appear in
first. - Each shared number appears once.
Test data
TEST DATA 1: first is [4, 8, 8, 2, 9], second is [9, 2, 5, 8]
TEST DATA 2: first is [1], second is []
Practice focus
Nested loops compare every pair and build rows. The slow, simple answer to most problems is two loops. Knowing it is what lets you see how to beat it.