Given two slices of integers, return the values that appear in both, with no duplicates, sorted in ascending order.
intersection(a, b)
Input format: a | b — two space-separated integer lists divided by a pipe.
Output the shared values, sorted, space-separated (empty line if none).
intersection([]int{1, 2, 3, 4}, []int{3, 4, 5}) // → [3, 4]
intersection([]int{1, 2}, []int{3, 4}) // → []
intersection([]int{2, 2, 3}, []int{2, 3, 3}) // → [2, 3]
You want the values shared between two lists, with no duplicates in the answer. The brute-force instinct: for every value in the first list, scan the whole second list to check if it's there. That works, but the total work grows with the product of the two lists' sizes — expensive fast as either grows.
The brute-force approach spends nearly all its time answering the same question: "is this value present in the other list?" That's a membership question, and a hash set answers those in constant time on average, versus scanning a list in time proportional to its length.
Building the set costs time proportional to the first list's size; scanning the second list with constant-time lookups costs time proportional to its size. Together that's linear in the combined sizes, instead of the multiplicative cost of comparing every pair. The tradeoff is extra memory for the set — usually a good deal for a large cut in running time.
"Check and remove" beats "check and mark used" with a separate structure: removing from the set is a single O(1) operation that both answers "was it there?" and prevents reuse, with no extra bookkeeping.
def intersection(a, b):
seen = set(a)
res = []
for v in b:
if v in seen:
res.append(v)
seen.discard(v)
res.sort()
return res
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.