Skip to content

Array sorting problem

anoop kumar

Asked Sep 24, 2018, 6:33 AM EDT1 answer
Share
LinkedInX
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();
}
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi Anoop, 
     
    You can try this code
     
    Java
    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();
     
    0