Tutorialspoint
Problem
Solution
Submissions

Intersection of Two Arrays

Certification: Basic Level Accuracy: 50% Submissions: 4 Points: 5

Write a C# program to find the intersection of two arrays. The intersection of two arrays is the set of elements that are common to both arrays.

Example 1
  • Input: nums1 = [1,2,2,1], nums2 = [2,2]
  • Output: [2,2]
  • Explanation: The elements that appear in both arrays are [2,2].
Example 2
  • Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
  • Output: [4,9]
  • Explanation: The elements that appear in both arrays are [4,9]. Note that the order doesn't matter.
Constraints
  • 1 ≤ nums1.length, nums2.length ≤ 1000
  • 0 ≤ nums1[i], nums2[i] ≤ 1000
  • Time Complexity: O(n + m)
  • Space Complexity: O(min(n, m))
ArraysHCL TechnologiesLTIMindtree
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a dictionary or a hash set to count occurrences of elements in the first array.
  • Iterate through the second array and check for matches.
  • For each match found, add it to the result and decrease the count in the dictionary.
  • Consider which array to use for building the dictionary (the smaller one is usually more efficient).


Submitted Code :