pass a copy of input array as argument so we don't modify original array