Romil N Romil N - 29 days ago 6
C# Question

Reverse an array without using reverse method of array

How to reverse an array (in C#) without using Array.Reverse method?

Say for example,

int[] arr ={1,3,4,9,8}


should result in

8,9,4,3,1


Thanks!

Answer
for (int i = 0; i < arr.Length / 2; i++)
{
   int tmp = arr[i];
   arr[i] = arr[arr.Length - i - 1];
   arr[arr.Length - i - 1] = tmp;
}