Problem · Hash Table
Most Common Product Pair
Learn this problemProblem statement
You are given a list of transactions, where each transaction is a list of products purchased together.
Return the pair of products that appears most frequently across all transactions.
Rules:
- A pair consists of exactly two distinct products.
(A, B)and(B, A)are considered the same pair — always return it in lexicographic order. - If multiple pairs have the same highest frequency, return the lexicographically smallest pair.
- Duplicate items within a transaction should be counted only once.
- Each transaction can contribute multiple pairs.
Function
mostCommonProductPair(transactions: String[][]) → String[]Examples
Example 1
transactions = [["A","B"],["A","B"],["A","C"]]return = ["A","B"]Pairs across all transactions: AB appears in transaction 1 and 2 (count=2), AC appears in transaction 3 (count=1). Most frequent pair is (A, B). Return ["A","B"].
Example 2
transactions = [["A","B","C"],["A","B"],["B","C"],["A","C"]]return = ["A","B"]Transaction 1 contributes pairs AB, AC, BC. Transactions 2–4 contribute AB, BC, AC respectively. Counts: AB=2, AC=2, BC=2 — all tied. Return the lexicographically smallest pair: ["A","B"].
Constraints
1 <= transactions.length <= 10^31 <= transactions[i].length <= 100