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
Comparing arrays
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
giorgi dvalishvili2 pointsPosted Nov 8, 2013, 10:53 PM EST
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); } } }csharp beginner24 pointsPosted Nov 9, 2013, 12:24 AM EST
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