Thursday, May 28, 2015

L Question: Intersection for Two Arrays

List<int> FindIntersection(int[] A, int[] B)
{
         int L = A.Length;
         int K = B.Length;

         List<int> intersectionArr = new List<int>();
         int i = L - 1;
         int j = K - 1;
         
          while ((i >= 0) && (j >=  0))
          {
                 if (A[i] > B[j])
                 {
                        i--;
                 }
                 else if (B[j] > A[i])
                 {
                         j--;
                 }
                 else
                 {
                         intersectionArr.Add(A[i]);
                          i--;
                          j--;
                        
                 }
          }
          return intersectionArr;
}

No comments:

Post a Comment