Rank Among Values Occurring Once Across Two Lists
Arrays & HashingAuto-graded · 20 test casesLast updated
Count occurrences across two combined lists, keep values seen exactly once, and return the k-th largest of them or -1 if there are fewer than k.
You are given two integer lists, first and second, and a positive integer k.
Treat the two lists as one pool of numbers. Call a value a singleton if it occurs exactly one time in the whole pool, counting occurrences in both lists together. A value that appears twice in one list, or once in each list, is not a singleton.
Order the singleton values from largest to smallest. Return the one in position k (counting from 1). If the pool has fewer than k singleton values, return -1.
Details
- Occurrences are counted over both lists combined, not per list.
- Each singleton value is considered once in the ranking.
- One or both lists may be empty.
- Values may be negative. The return value
-1is the required result when there are fewer thanksingletons, even though-1can also be a genuine singleton value. Follow the rule exactly as stated.
Constraints
0 <= len(first), len(second) <= 10^5- Every value is in the range
[-10^6, 10^6] 1 <= k <= 10^5
Examples
Example 1
Input: kth_largest_singleton([3, 2, 1, 3], [4, 1, 5], 2)
Output: 4
Example 2
Input: kth_largest_singleton([1, 1], [2, 2], 1)
Output: -1
Hints
Hint 1
Whether a value qualifies depends on its total count across both lists, so checking each list separately is not enough.
Hint 2
Build a frequency map over the combined data, then keep only the values whose frequency is exactly 1.
Hint 3
Sort the surviving values in descending order and read index k - 1, after checking that k does not exceed their number. A size-k min-heap also works.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Whether a value is a singleton is a property of the combined data, so we cannot judge each list on its own.
- Count every value across both lists using one hash map.
- Keep only the values whose count is exactly 1.
- Sort those values in descending order. If at least
kremain, return the one at indexk - 1; otherwise return-1.
Why it works
Counter.updateadds counts rather than replacing them, so each key's final count equals its total number of occurrences in both lists.- Filtering on count 1 yields exactly the singleton values, each listed once because we iterate over map keys.
- After a descending sort, index 0 is the largest and index
k - 1is the k-th largest. Ifkis larger than the number of singletons, no such element exists and the answer is-1. - Empty lists, negative values, and the case of no singletons all follow from the same logic.
Complexity
Let n and m be the list lengths and u the number of singletons (u <= n + m).
- Time: O(n + m + u log u).
- Space: O(n + m) for the counter, plus O(u) for the sorted list.
A min-heap of size k over the singletons gives O(u log k) time when k is small. Because values lie in a bounded range, a counting array over that range with a top-down scan is another option.
Reference solution
from collections import Counter
from typing import List
def kth_largest_singleton(first: List[int], second: List[int], k: int) -> int:
counts = Counter(first)
counts.update(second)
singles = sorted((v for v, c in counts.items() if c == 1), reverse=True)
return singles[k - 1] if k <= len(singles) else -1