Meital Hilf

Meital Hilf

  • NA
  • 7
  • 428

C# question to get a new array

Apr 26 2023 6:43 PM

hi, I have a question in #c
I wrote a code but the printing is not correct.  In the code there is an operation that receives two arrays of type int and returns a new array of type int whose values ??are all the values ??common to the two previous arrays

int[] one = new int[] { 4, 5, 6, 7 };
int[] two = new int[] { 4, 5, 7, 8 };

static int[] twoArrays(int[] one, int[] two)
{
    int[] newArray = new int[one.Length];
    int x = 0;

    for (int i = 0; i < one.Length; i++)
    {
        for (int k = 0; k < two.Length; k++)
        {
            if (one[i] == two[i])
            {
                newArray[x] = one[i];
                x++;
            }
        }           

    }           

return newArray;

}
Console.WriteLine(twoArrays(one, two));


Answers (2)