Intersection of Multiple Arrays - Problem

Given a 2D integer array 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.

Each sub-array contains only distinct positive integers, and we need to find the intersection of all arrays - the numbers that appear in every single array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
Output: [3,4]
💡 Note: Numbers 3 and 4 appear in all three arrays: [3,1,2,4,5] contains 3,4; [1,2,3,4] contains 3,4; [3,4,5,6] contains 3,4
Example 2 — Single Element
$ Input: nums = [[1,2,3],[4,5,6]]
Output: []
💡 Note: No numbers appear in both arrays - first array has [1,2,3] and second has [4,5,6] with no overlap
Example 3 — All Same
$ Input: nums = [[1,2,3,4,5],[1,2,3,4,5]]
Output: [1,2,3,4,5]
💡 Note: Both arrays are identical, so all elements [1,2,3,4,5] appear in every array

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i].length ≤ 1000
  • 1 ≤ nums[i][j] ≤ 1000
  • All the values of nums[i] are unique

Visualization

Tap to expand
Intersection of Multiple ArraysFind elements present in ALL arrays[3,1,2,4,5][1,2,3,4][3,4,5,6]Array 1Array 2Array 3341256Not in allIn all arraysNot in allIn all arraysNot in allNot in allOutput: [3,4]
Understanding the Visualization
1
Input
Multiple arrays with distinct elements
2
Process
Find elements present in ALL arrays
3
Output
Sorted list of common elements
Key Takeaway
🎯 Key Insight: Count frequency of each number - elements in intersection appear exactly once per array
Asked in
Amazon 25 Facebook 18 Google 12
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen