shifting array elements to right?

The easiest way to go:

public int[] shiftLeft(int[] arr) 
{
    int[] demo = new int[arr.Length];

    for (int i = 0; i < arr.Length - 1; i++) 
    {
        demo[i] = arr[i + 1];
    }

    demo[demo.Length - 1] = arr[0];

    return demo;
}

public int[] shiftRight(int[] arr) 
{
    int[] demo = new int[arr.Length];

    for (int i = 1; i < arr.Length; i++) 
    {
        demo[i] = arr[i - 1];
    }

    demo[0] = arr[demo.Length - 1];

    return demo;
}

//right shift with modulus
for (int i = 0; i < arr.length; i++) {
    demo[(i+1) % demo.length] = arr[i];
}

Tags:

C#