Home › Company-wise questions › PayPal › Intersection of Two Arrays II ← PayPal bank
PayPal · Question 12 · Arrays
Intersection of Two Arrays II Arrays Java code ⏱ O(n+m) 💾 O(min(n,m))
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Return the intersection of two arrays including duplicates.
Approach Use a HashMap to count frequencies.
Time O(n+m)Space O(min(n,m))
How it works 1 Count nums1 frequencies. 2 Spend counts matching nums2 in order. Sample input / output Input:
nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output:
[2, 2] Constraints 1 <= lengths <= 1000; result in any order.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n+m)Space O(min(n,m))Loops 3Branches 1Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat yes ↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE static int[] intersect(int[] nums1, int[] nums2) Map<Integer, Integer> map = new HashMap<>() for (int num : nums1) map.put(num, map.getOrDefault(num, 0) + 1) List<Integer> result = new ArrayList<>() for (int num : nums2) if (map.getOrDefault(num, 0) > 0) result.add(num) map.put(num, map.get(num) - 1) DEFINE [] res = new int[result.size()] for (int i = 0; i < result.size(); i++) res[i] = result.get(i) RETURN res DEFINE static void main(String[] args) System.out.println(Arrays.toString(intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2}))) END start / end statement condition loop returns definition← Q11 · Contains Duplicate II Q13 · Plus One → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →