Skip to content

Commit 69e2528

Browse files
Merge pull request neetcode-gh#2987 from dkiryanov/csharp/2215-find-the-difference-of-two-arrays
Added a solution (C#) to 2215 find the difference of two arrays
2 parents 6cc1e3f + 268332c commit 69e2528

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution
2+
{
3+
public int StrStr(string haystack, string needle)
4+
{
5+
if (needle.Length > haystack.Length) return -1;
6+
7+
int l = 0;
8+
int r = needle.Length - 1;
9+
10+
while (r < haystack.Length)
11+
{
12+
bool areEqual = true;
13+
for (int i = l, j = 0; i <= r; i++, j++)
14+
{
15+
if (haystack[i] != needle[j])
16+
{
17+
areEqual = false;
18+
break;
19+
}
20+
}
21+
22+
if (areEqual)
23+
{
24+
return l;
25+
}
26+
27+
l++;
28+
r++;
29+
}
30+
31+
return -1;
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution
2+
{
3+
public IList<IList<int>> FindDifference(int[] nums1, int[] nums2)
4+
{
5+
HashSet<int> set1 = new HashSet<int>(nums1);
6+
HashSet<int> set2 = new HashSet<int>(nums2);
7+
8+
IList<IList<int>> res = new List<IList<int>>() { new List<int>(), new List<int>() };
9+
10+
foreach (var el in set1)
11+
{
12+
if (set2.Contains(el)) continue;
13+
14+
res[0].Add(el);
15+
}
16+
17+
foreach (var el in set2)
18+
{
19+
if (set1.Contains(el)) continue;
20+
21+
res[1].Add(el);
22+
}
23+
24+
return res;
25+
}
26+
}

0 commit comments

Comments
 (0)