Intersection of Three Sorted Arrays — LeetCode 1213 Python Solution
EasyLeetCode PremiumArrayHash TableBinary SearchCounting
- Problem
- #1213
- Pattern
- Binary Search
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Example
- Input
- arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
- Output
- [1,5]
- Explanation
- Only 1 and 5 appeared in the three arrays.
Python solution
Python
class Solution:
def arraysIntersection(
self, arr1: List[int], arr2: List[int], arr3: List[int]
) -> List[int]:
cnt = Counter(arr1 + arr2 + arr3)
return [x for x in arr1 if cnt[x] == 3]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(m) auxiliary |
Pattern: Binary Search
Halve the search space each step — over an array, or over the answer itself. LeetCode 1213. Intersection of Three Sorted Arrays is filed here because LeetCode tags it Binary Search, which is the vocabulary this hub collects.
The binary search guide has the Python template for the pattern and the 254 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1213. Intersection of Three Sorted Arrays?
- LeetCode 1213. Intersection of Three Sorted Arrays is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1213. Intersection of Three Sorted Arrays?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1213. Intersection of Three Sorted Arrays?
- The Python solution on this page uses O(m) auxiliary space.
- What topics does LeetCode 1213. Intersection of Three Sorted Arrays cover?
- LeetCode 1213. Intersection of Three Sorted Arrays is tagged Array, Hash Table, Binary Search and Counting on LeetCode.
- Is LeetCode 1213. Intersection of Three Sorted Arrays a premium problem?
- Yes. LeetCode 1213. Intersection of Three Sorted Arrays is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.