Skip to content

Comparing arrays

csharp beginner

Asked Nov 8, 2013, 10:02 PM EST2 answers
Share
LinkedInX
i have three arrays, say, array1, array2, array3. 

array1 = {10, 11, 12, 13, 14 , 15, 16, 17, 18, 19, 20}

array2 = {10, 11, 13, 16, 19, 20}
array3 = {22, 25, 75, 36, 82, 99}

i want to check: if 'ith' element from array1 is present in array2, then write 'ith' element of array3, corresponding to array2, else, put a null value.

Note:
# array2.length = array3.length

#The result should be like:
        10        22
        11        25
        12        null
        13        75
        14        null
        15        null
        16        36
        17        null
        18        null
        19        82
        20        99

can anybody help?

Regards

2 Answers

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

  • 0
    C#
    class Program
        {
            static void Main(string[] args)
            {
                int[] arr1 = new int[] { 1, 2, 3, 4, 5 ,6,7,8,9,10,17};
                int[] arr2 = new int[] { 1, 2, 7,0, 9, 11 };
    
    
                int[] arr3 = new int[] { 10, 11,12, 13, 14,15 };
    
    
                for (int i = 0; i < arr2.Length; i++)
                {
                    if (!arr1.Contains<int>(arr2[i]))
                    {
    
    
                        arr3[i] = 0;
                    }
                }
                foreach (var item in arr3)
                {
                    Console.WriteLine(item);
                }
            }
        }
    0
  • 0
    Giorgi, thanks for your effort, but it is not solving my problem. I want for missing values of arr1 in arr2, i could have a null value in arr3. Actually, arr2 and arr3 are columnA and ColumnB values respectively, of my file.

    That is why i gave:
    # The result should be like:
            10    22
            11    25
            12    null
            13    75
            14    null
            15    null
            16    36
            17    null
            18    null
            19    82
            20    99
    0