Skip to content

Commit 41eb2ef

Browse files
committed
#2956: Find Common Elements between Two Arrays; solution & tests
1 parent 23a6606 commit 41eb2ef

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* 2956
3+
* Find Common Elements Between Two Arrays
4+
**
5+
* You are given two integer arrays nums1 and nums2 of sizes n and m, respectively.
6+
* Calculate the following values:
7+
* • answer1 : the number of indices i such that nums1[i] exists in nums2.
8+
* • answer2 : the number of indices i such that nums2[i] exists in nums1.
9+
*
10+
* Return [answer1,answer2].
11+
*
12+
* Example 1:
13+
* Input: nums1 = [2,3,2], nums2 = [1,2]
14+
* Output: [2,1]
15+
*
16+
* Example 2:
17+
* Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
18+
* Output: [3,4]
19+
* Explanation:
20+
* The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3.
21+
* The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4.
22+
*
23+
* Example 3:
24+
* Input: nums1 = [3,4,2,3], nums2 = [1,5]
25+
* Output: [0,0]
26+
* Explanation:
27+
* No numbers are common between nums1 and nums2, so answer is [0,0].
28+
*
29+
* Constraints:
30+
* • n == nums1.length
31+
* • m == nums2.length
32+
* • 1 <= n, m <= 100
33+
* • 1 <= nums1[i], nums2[i] <= 100
34+
*
35+
* Hint 1:
36+
* Since the constraints are small,
37+
* you can use brute force to solve the problem.
38+
*
39+
* Hint 2:
40+
* For each element i in nums1,
41+
* iterate over all elements of nums2 to find if it occurs.
42+
**
43+
* https://leetcode.com/problems/find-common-elements-between-two-arrays/
44+
***/
45+
46+
namespace Problems;
47+
48+
public class FindCommonElementsBetweenTwoArrays
49+
{
50+
public int[] FindIntersectionValues( int[] nums1, int[] nums2 )
51+
{
52+
bool[] freq1 = new bool[101];
53+
bool[] freq2 = new bool[101];
54+
55+
foreach ( int num in nums1 )
56+
{
57+
freq1[num] = true;
58+
}
59+
60+
int numsCount2 = 0;
61+
62+
foreach ( int num in nums2 )
63+
{
64+
freq2[num] = true;
65+
66+
if ( freq1[num] )
67+
{
68+
numsCount2++;
69+
}
70+
}
71+
72+
int numsCount1 = 0;
73+
74+
foreach ( int num in nums1 )
75+
{
76+
if ( freq2[num] )
77+
{
78+
numsCount1++;
79+
}
80+
}
81+
82+
return [numsCount1, numsCount2];
83+
}
84+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using NUnit.Framework;
2+
3+
using Problems;
4+
5+
public class FindCommonElementsBetweenTwoArraysTests
6+
{
7+
[TestCase( new int[] { 2, 3, 2 }, new int[] { 1, 2 }, ExpectedResult = new int[] { 2, 1 } )]
8+
[TestCase( new int[] { 4, 3, 2, 3, 1 }, new int[] { 2, 2, 5, 2, 3, 6 }, ExpectedResult = new int[] { 3, 4 } )]
9+
[TestCase( new int[] { 3, 4, 2, 3 }, new int[] { 1, 5 }, ExpectedResult = new int[] { 0, 0 } )]
10+
public int[] FindIntersectionValuesTest( int[] nums1, int[] nums2 ) =>
11+
new FindCommonElementsBetweenTwoArrays().FindIntersectionValues( nums1, nums2 );
12+
}

0 commit comments

Comments
 (0)