Manhattan Associates Interview Question

remove element from array which is at specified index

Interview Answer

Anonymous

Apr 20, 2019

// Index to be removed int index = 5; int[] input = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] result = new int[input.length - 1]; // Solution 1 System.arraycopy(input, 0, result, 0, index); System.arraycopy(input, index + 1, result, index, result.length - index); System.out.println(Arrays.toString(result)); // Solution 2 int[] output = IntStream .range(0, input.length) .filter(i -> i != index) .map(i -> input[i]) .toArray(); System.out.println(Arrays.toString(output));