Skip to content

Find Elements Present In First Array But Missing Second Array Using For Loop

In order to execute the code I’ve created a simple console application to print out the result.

Please find complete code below:

int[] firstArray = { 1, 2, 6, 3, 4, 5 };
int[] secondArray = { 2, 4, 3, 1, 0 };

using System;
using System.Text;

namespace PracticeConsole
{
   class Program
   {
      static void Main(string[] args)
      {
         StringBuilder missingNumbers = new StringBuilder();
         for (int i = 0; i < firstArray.Length; i++)
         {
            int j;
            for (j = 0; j < secondArray.Length; j++)
            {
               if (firstArray[i] == secondArray[j])
               {
                  break;
               }
            }
            if (j == secondArray.Length)
            {
               missingNumbers = missingNumbers.Append(firstArray[i] + ", ");
            }
         }
         Console.WriteLine(missingNumbers.ToString(0, missingNumbers.Length - 2));
         Console.ReadLine();
      }
   }
}

Result: