I have 3 array of int data type and i want to find common numbers from arrays without using standard method.
I tried this:--
C#
class Program
{
static void Main(string[] args)
{
int[] array1 = { 1, 4, 2, 8, 7 };
int[] array2 = { 7, 5, 9, 1, 0,2,15};
int[] array3 = { 7, 5, 9, 1, 0, 2, 15,56,23 };
for (int i=0; i<array1.Length;i++)
{
for (int j = 0; j < array2.Length; j++)
{
for (int k = 0; k < array1.Length; k++)
{
if (array1[i] == array2[j] || array1[j] == array2[k])
{
Console.WriteLine(array1[i]);
}
}
}
}
Console.ReadKey();
}
}
Uzma Khan152 pointsPosted Sep 24, 2018, 10:07 PM EDT
int[] array1 = { 1, 4, 2, 8, 7 }; int[] array2 = { 7, 5, 9, 1, 0, 2, 15 }; int[] array3 = { 7, 5, 9, 1, 0, 2, 15, 56, 23 }; List<int> temp = new List<int>(), output = new List<int>(); for (int i = 0; i < array1.Length; i++) { for (int j = 0; j < array2.Length; j++) { if (array1[i] == array2[j]) temp.Add(array1[i]); } } output = temp; temp = new List<int>(); for (int i = 0; i < array3.Length; i++) { for (int j = 0; j < array2.Length; j++) { if (array3[i] == array2[j]) temp.Add(array3[i]); } } var t = output.Concat(temp).Distinct().ToList(); t.ForEach(i => Console.Write("{0}\t", i)); Console.ReadKey();