Array Slicing

In short: Slicing extracts a contiguous portion of an array or string into a new sequence, given a start and end index. It's concise and readable, but copying the elements takes O(k) time and O(k) space for a slice of length k.

Array slicing involves taking a subset from an array and allocating a new array with those elements.

In Java you can create a new array of the elements in myArray, from startIndex to endIndex (exclusive), like this:

Arrays.copyOfRange(myArray, startIndex, endIndex);

Careful: there's a hidden time and space cost here! It's tempting to think of slicing as just "getting elements," but in reality you are:

  1. allocating a new array
  2. copying the elements from the original array to the new array

This takes time and space, where n is the number of elements in the resulting array.

That's a bit easier to see when you save the result of the slice to a variable:

int[] tailOfArray = Arrays.copyOfRange(myArray, 1, myArray.length);

But a bit harder to see when you don't save the result of the slice to a variable:

return Arrays.copyOfRange(myArray, 1, myArray.length); // whoops, I just spent O(n) time and space!
for (int item : Arrays.copyOfRange(myArray, 1, myArray.length)) { // whoops, I just spent O(n) time and space! }

So keep an eye out. Slice wisely.

Frequently Asked Questions

What is array slicing?

Slicing creates a new array or string containing a contiguous range of elements from an existing one, specified by start and end indices.

What is the time complexity of slicing?

O(k) time and O(k) space, where k is the number of elements copied, because slicing builds a new sequence rather than referencing the original.

Does slicing modify the original array?

Usually no. In most languages slicing returns a new copy and leaves the original unchanged, though the exact behavior depends on the language.

Last updated: June 17, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .