All problems
0730EasyArrayHash Table

Tokens Shared By Two Crates

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2956Find Common Elements Between Two Arrays

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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

  • a is the number of positions i in left whose code appears somewhere in right, and
  • b is the number of positions j in right whose code appears somewhere in left.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def shared_tallies(left: list[int], right: list[int]) -> list[int]:
Java
public int[] sharedTallies(int[] left, int[] right)
September 7
Apply