## The challenge

Create a method take that accepts a list/array and a number n, and returns a list/array array of the first n elements from the list/array.

If you need help, here’s a reference:

## Test cases

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 `````` ``````import org.junit.Test; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; import java.util.*; public class SolutionTest { @Test public void basicTests() { assertArrayEquals("should return the first 3 items", new int[]{0, 1, 2}, ZywOo.take(new int[]{0, 1, 2, 3, 5, 8, 13}, 3)); assertArrayEquals("should return 0 items", new int[0], ZywOo.take(new int[]{0, 1, 2, 3, 5, 8, 13}, 0)); assertArrayEquals("empty array should return empty array", new int[0], ZywOo.take(new int[0], 3)); } @Test public void randomTests() { for (int i = 0; i < 100; i++) { int n = (int)Math.floor(100 * Math.random()); int[] randomArray = generateRandomArray(); assertArrayEquals("Testing with array " + Arrays.toString(randomArray) + " and n = " + n, solution(randomArray, n), ZywOo.take(randomArray, n)); } } private int[] solution(int[] arr, int n) { return arr.length > 0 && arr.length > n ? Arrays.copyOfRange(arr, 0, n) : arr; } private int[] generateRandomArray() { int length = (int)Math.floor(100 * Math.random()); int min = (int)Math.floor(-100 * Math.random()); int max = (int)Math.floor( 100 * Math.random()); return new Random().ints(length, min, max).toArray(); } } ``````

## The solution in Java

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 `````` ``````public class ZywOo { public static int[] take(int[] arr, int n) { if (arr.length==0) return arr; if (n>arr.length) return arr; int[] arr2 = new int[n]; for (int i=0; i

Option 2 (using `copyOf`):

 ``````1 2 3 4 5 6 7 `````` ``````import java.util.Arrays; class ZywOo { static int[] take(int[] arr, int n) { return n > arr.length ? arr : Arrays.copyOf(arr, n); } } ``````

Option 3 (using `streams`):

 ``````1 2 3 4 5 6 `````` ``````import java.util.Arrays; public class ZywOo { public static int[] take(int[] arr, int n) { return Arrays.stream(arr).limit(n).toArray(); } } ``````

Option 4 (using `copyOfRange`):

 ``````1 2 3 4 5 6 7 `````` ``````import java.util.Arrays; public class ZywOo { public static int take(int[] arr, int n)[] { return Arrays.copyOfRange(arr, 0, Math.min(arr.length, n)); } } ``````