Union, Intersection and Diff of two sorted arrays in C#

This is the original question https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/

Given two sorted arrays, find their union and intersection.

Example:

Input : arr1[] = {1, 3, 4, 5, 7}
arr2[] = {2, 3, 5, 6}
Output : Union : {1, 2, 3, 4, 5, 6, 7}
Intersection : {3, 5}

Input : arr1[] = {2, 5, 6}
arr2[] = {4, 6, 8, 10}
Output : Union : {2, 4, 5, 6, 8, 10}
Intersection : {6}

I also added one more case of finding items which are only in one of the two arrays and called it Diff.

Please review for performance.
Please do not comment about code in the same class as the test and the functions not being static. It is just faster for me like this to get to the point of the exercise.

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{
/// <summary>
/// https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/
/// </summary>

[TestClass]
public class UnionAndIntersectionOfTwoSortedArrays2
{
[TestMethod]
public void UnionTest()
{
int[] arr1 = { 1, 3, 4, 5, 7 };
int[] arr2 = { 2, 3, 5, 6 };
int[] union = { 1, 2, 3, 4, 5, 6, 7 };
CollectionAssert.AreEqual(union, Union(arr1, arr2));
}

[TestMethod]
public void IntersectionTest()
{
int[] arr1 = { 1, 3, 4, 5, 7 };
int[] arr2 = { 2, 3, 5, 6 };

int[] intersection = { 3, 5 };
CollectionAssert.AreEqual(intersection, Intersection(arr1, arr2));
}

[TestMethod]
public void DiffTest()
{
int[] arr1 = { 1, 3, 4, 5, 7 };
int[] arr2 = { 2, 3, 5, 6 };

int[] diff = { 1, 2, 4, 6, 7 };
CollectionAssert.AreEqual(diff, Diff(arr1, arr2));
}

private int[] Diff(int[] arr1, int[] arr2)
{
int i = 0;
int j = 0;
int n = arr1.Length;
int m = arr2.Length;
List<int> list = new List<int>();
while (i < n && j < m)
{
if (arr1[i] == arr2[j])
{
i++;
j++;
}

else if (arr1[i] < arr2[j])
{
i++;
}
else
{
j++;

}
}

while (i < n)
{
i++;
}
while (j < m)
{
j++;
}
return list.ToArray();
}

private int[] Intersection(int[] arr1, int[] arr2)
{
int i = 0;
int j = 0;
int n = arr1.Length;
int m = arr2.Length;
List<int> list = new List<int>();
while (i < n && j < m)
{
if (arr1[i] == arr2[j])
{
i++;
j++;
}
else if (arr1[i] < arr2[j])
{
i++;
}
else
{
j++;
}
}

return list.ToArray();
}

public int[] Union(int[] arr1, int[] arr2)
{
int i = 0;
int j = 0;
int n = arr1.Length;
int m = arr2.Length;
List<int> list = new List<int>();
while (i < n && j < m)
{
if (arr1[i] < arr2[j])
{
i++;
}
else if (arr2[j] < arr1[i])
{
j++;
}
else // equals
{
i++;
j++;

}
}
//handle the rest
for (; i < n; i++)
{
}
for (; j < m; j++)
{
}

return list.ToArray();
}
}
}
• It is just faster for me like this to get to the point of the exercise. If you code really fast, would you also expect a fast review? – dfhwze Jul 4 at 4:33
• @dfhwze I mean to say that please disregard why it is not separate class for test and for the rest of the code. – Gilad Jul 4 at 6:26
• I get what you mean, but still it would be a very small effort to extract the algorithm to a separate class and call that class in the unit tests. – dfhwze Jul 4 at 6:34
• It looks like both Union() and Intersection() are the basic implementation of the algorithms which do not handle duplicates (Union of { 1, 2, 2, 2, 3 } and { 2, 3, 4, 5 }) gives {1, 2, 2, 2, 3, 4, 5} not {1, 2, 3, 4, 5} and Intersection of { 2,5,5,6,6} and {4,6,6,8,10} gives {6,6} not {6}) is this intended? – AlanT Jul 4 at 8:44
• I see you are practicing various algorithms but writing working code is a no-brainer. Doing it in a way that can be maintained, tested and easily understood, with intuitive API is much much harder and on higher levels this is what counts most. Currently you just write something to solve the task avoiding to implement additional types to encapsulate the logic in proper modules. If you want to improve your skills you should try to write more professional code with proper types, names etc. I would still classify this code as beginner. – t3chb0t Jul 4 at 19:11