Show problems as
Classic problem #2956EasyArrayHash Table

Find Common Elements Between Two Arrays

Real-world version: Tokens Shared By Two Crates
Tracked in this browser only
Read Find Common Elements Between Two Arrays on LeetCode(opens in a new tab)

The 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

  • 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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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