Find Common Elements Between Two Arrays
Real-world version: Tokens Shared By Two CratesThe statement here is our own: the same inputs, outputs and technique.
Two crates of tokens have been logged. left lists the colour code of each token in the first crate and right does the same for the second crate.
Return a two-entry array [a, b], where
ais the number of positionsiinleftwhose code appears somewhere inright, andbis the number of positionsjinrightwhose code appears somewhere inleft.
How often a code is repeated in the other crate does not matter, only whether it is there at all. Positions are counted one by one, so a crate that holds the same code three times can contribute three to its tally.
Examples
Example 1
- Input
- left = [1, 1, 2], right = [1, 3, 3]
- Output
- [2, 1]
Code 1 sits at two positions of the first crate and appears in the second, so the first tally is 2. Code 2 does not appear in the second crate. Looking the other way, only the single 1 in the second crate has a match, so the second tally is 1.
Example 2
- Input
- left = [5, 5, 6], right = [6, 6, 5, 5]
- Output
- [3, 4]
Both crates hold codes 5 and 6, so every position of both crates counts: three in the first crate and four in the second.
Example 3
- Input
- left = [7, 8, 9], right = [4, 5, 6]
- Output
- [0, 0]
The crates have no code in common, so both tallies are 0.
Constraints
- 1 <= left.length <= 100
- 1 <= right.length <= 100
- 1 <= left[i] <= 100
- 1 <= right[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def shared_tallies(left: list[int], right: list[int]) -> list[int]:public int[] sharedTallies(int[] left, int[] right)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's