Skip to main content
1 of 2
Gilad
  • 5.3k
  • 5
  • 37
  • 63

Union and Intersection and Diff of two sorted arrays 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 if one of the two array 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])
                {
                    list.Add(arr1[i]);
                    i++;
                }
                else
                {
                    list.Add(arr2[j]);
                    j++;

                }
            }

            while (i < n)
            {
                list.Add(arr1[i]);
                i++;
            }
            while (j < m)
            {
                list.Add(arr2[j]);
                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])
                {
                    list.Add(arr1[i]);
                    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])
                {
                    list.Add(arr1[i]);
                    i++;
                }
                else if (arr2[j] < arr1[i])
                {
                    list.Add(arr2[j]);
                    j++;
                }
                else // equals
                {
                    list.Add(arr1[i]);
                    i++;
                    j++;

                }
            }
            //handle the rest
            for (; i < n; i++)
            {
                list.Add(arr1[i]);
            }
            for (; j < m; j++)
            {
                list.Add(arr2[j]);
            }

            return list.ToArray();
        }
    }
}
Gilad
  • 5.3k
  • 5
  • 37
  • 63