nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Example 1:
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Example 2:
Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
Constraints:
1 <= nums.length <= 10001 <= sum(nums[i].length) <= 10001 <= nums[i][j] <= 1000nums[i] are unique.Problem Overview: You receive a list of integer arrays. The task is to return all numbers that appear in every array. The final result must be sorted in ascending order. This is essentially computing the intersection across multiple arrays rather than just two.
Approach 1: Hash Map Counting (O(N) time, O(U) space)
This approach counts how many arrays contain each value. Iterate through every number in every array and maintain a frequency map using a hash table. The key insight is that a value belongs in the final intersection only if its count equals the number of arrays. After counting, iterate through the map and collect numbers whose frequency equals nums.length. Since the output must be sorted, apply a final sort step on the result list.
Hash table lookups and updates run in constant time on average, making the overall counting phase linear with respect to the total number of elements across all arrays. This approach works well for the general case and handles duplicate values naturally because the counting logic focuses on array presence rather than position.
Relevant concepts: hash table, counting, and array.
Approach 2: Set Intersection (O(N) time, O(U) space)
Another clean strategy uses set intersections. Convert the first array into a set, then iteratively intersect it with sets created from each remaining array. Each intersection keeps only elements that exist in both sets. After processing all arrays, the resulting set contains values common to every array.
This method works because set intersection removes any element missing from even one array. Most languages provide optimized set operations that run in roughly linear time relative to the number of elements involved. Once the final set is computed, convert it into a list and sort it to satisfy the problem's ordering requirement.
This approach is often shorter to implement and easier to reason about compared with manual counting. It relies on standard hash table backed sets and simple intersection operations. Sorting is still required at the end, so the final step uses typical sorting algorithms.
Recommended for interviews: Interviewers typically expect the hash map counting solution. It demonstrates understanding of frequency tracking and how to aggregate information across multiple arrays. The set intersection approach is equally valid and often more concise, but explaining the counting logic clearly shows strong command of hash-based techniques used in many array problems.
This approach involves using a hash map to count the occurrences of each integer across all sub-arrays. By iterating through each integer array, we populate the hash map with the count data. At the end, any integer that appears the same number of times as there are sub-arrays is included in the result, as it indicates the integer is present in each array.
The solution utilizes an array of size 1001 to count occurrences of each number across sub-arrays, tracking each number's presence uniquely within a sub-array to prevent counting a number multiple times per array. The result is formed by gathering numbers that appear in all sub-arrays.
Time Complexity: O(n), where n is the total number of elements across all arrays. Space Complexity: O(1), since the auxiliary space used (count array) is fixed.
Sets provide built-in operations for finding intersections, so this approach initializes the set of result with elements from the first array, and iteratively intersects it with subsequent sets. This efficiently prunes elements not common to all arrays and provides automatic sorting for final output.
This Python approach uses the intersection operator '&=' of sets to retain only elements appearing in all sub-arrays. The result is converted to a sorted list before being returned for an intuitive solution.
Python
C++
Java
JavaScript
C#
Time Complexity: O(n log n) where n is the total number of unique elements due to sorting. Space Complexity: O(n) for storing sets.
Traverse the array nums. For each sub-array arr, count the occurrence of each number in arr. Then traverse the count array, count the numbers that appear as many times as the length of the array nums, which are the answers.
The time complexity is O(N), and the space complexity is O(1000). Where N is the total number of numbers in the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Using Hash Maps | Time Complexity: O(n), where n is the total number of elements across all arrays. Space Complexity: O(1), since the auxiliary space used (count array) is fixed. |
| Using Set Intersections | Time Complexity: O(n log n) where n is the total number of unique elements due to sorting. Space Complexity: O(n) for storing sets. |
| Counting | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Map Counting | O(N + K log K) | O(U) | General case when processing many arrays and counting element frequency |
| Set Intersection | O(N + K log K) | O(U) | Cleaner implementation when languages support fast set intersection |
Intersection of Multiple Arrays | Leetcode 2248 | Strings | Contest 290 🔥🔥 • Coding Decoded • 2,445 views views
Watch 9 more video solutions →Practice Intersection of Multiple Arrays with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor