You could use commons lang's ArrayUtils.

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

Answer from Peter Lawrey on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-an-element-at-specific-index-from-an-array-in-java
Remove an Element at Specific Index from an Array in Java - GeeksforGeeks
July 11, 2025 - Explanation: This example includes finding the element at the specified index and then removing that element. The rest of the elements are copied into a new array. This would lead to an array of size one less than the original array. We can use Java 8 streams to remove element at specific index from an array, when modifying small or mid-sized arrays.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-array-elements
Java Remove Array Elements: Methods and Examples | DigitalOcean
May 2, 2025 - To remove an element from an array by index in Java, you need to create a new array with the desired size, copy elements before the index to be removed to the new array, and then copy elements after the index to be removed to the new array, starting from the position of the element to be removed.
Discussions

How can i remove an element from array in Java - Stack Overflow
To remove an element from an array in Java, you need to create a new array and copy over all the elements you want to keep. That is because Java arrays are fixed-size. For example, to remove an element at a particular index, you could do it like this: More on stackoverflow.com
🌐 stackoverflow.com
Removing an element from an Array (Java) - Stack Overflow
0 How to remove elements from an array in java even if we have to iterate over array or can we do it directly? 0 Java 8 or higher, removing values from char array · 0 How can I delete an elment with it's index from one dimensional array in java? ... Why is it that Lacaille 8760 and Kepler 442 have approximately the same mass, but the former is a red dwarf and the latter is an orange dwarf? Which interpretation is correct if goal of our work is to get moksha by ... More on stackoverflow.com
🌐 stackoverflow.com
Remove specific index from array in java - Stack Overflow
Can I remove a specific element from array by mentioning index value? For example can I remove the character d by giving index value 1? char[] words = { 'c', 'd', 'f', 'h', 'j' }; More on stackoverflow.com
🌐 stackoverflow.com
December 28, 2015
Having trouble with java arrays, removing an element then returning a new size of an array
I’m on my phone so I’ll just explain what im thinking in pseudo code, but why don’t you just do something like: Create a new array of size n-1 where n is the size of the original array. Iterate over the old array in a for loop, adding each element to the new array. If the element at the ith position equals the element to remove, continue to the next iteration. Would that work? EDIT: I'm at my PC now so here's the code. I ended up doing it a little differently than I mentioned above. Note that it doesn't account for if the element appears multiple times in the array or when the element does not exist. public static void main(String []args) { int[] myArray = { 5,6,9,4 }; int[] newArray = removeElem(myArray, 9); for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i] + " "); } public static int[] removeElem(int[] array, int value) { int index = -1; // First confirm the element to remove exists in the array for (int i = 0; i < array.length; i++) { // If it does, store its index if (array[i] == value) index = i; } // If element exists if (index != -1) { int[] newArray = new int[array.length - 1]; for (int i = 0; i < array.length; i ++) { if (i < index) newArray[i] = array[i]; else if (i > index) newArray[i - 1] = array[i]; } return newArray; } // Do some error handling for if the element does not exist else return array; } Output is: 5 6 4 More on reddit.com
🌐 r/learnprogramming
17
4
July 13, 2018
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - The final argument is the number of elements to copy from the source array. The arraycopy is generally used to copy contents from some source array into some destination array. Though, we can also copy an array, or a part of it, into itself. This allows us to shift a part of it to the left like last time: int[] array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int index = 3; To remove the element, we only need to write this one line of code:
🌐
Stack Overflow
stackoverflow.com › questions › 59348581 › how-can-i-remove-an-element-from-array-in-java
How can i remove an element from array in Java - Stack Overflow
List<String> names = new ArrayList<>(Arrays.asList("Testname", "Charel", "Melissa", "Kelly")); names.remove("Melissa"); System.out.println(names); Output of both is the same as above.
🌐
w3resource
w3resource.com › java-exercises › array › java-array-exercise-7.php
Java - Remove a specific element from an array
May 9, 2025 - System.out.println("Original Array : " + Arrays.toString(my_array)); // Define the index of the element to be removed (second element, index 1, value 14). int removeIndex = 1; // Loop to remove the element at the specified index. for (int i ...
Find elsewhere
🌐
Coderanch
coderanch.com › t › 528600 › java › Remove-element-array-index
Remove an element from array by index (Beginning Java forum at Coderanch)
February 24, 2011 - LinkedList = 136,060,386 ArrayList = 19,373,657 LinkedList = 89,518,985 ArrayList = 18,704,275 ... LinkedLists are great when: - adding to the start or the end - removing from the start or the end - adding, removing or setting using a ListIterator All direct element accesses will require the list to be traversed until that element is found, and that's what makes it slow if you use any indexed method.
🌐
Baeldung
baeldung.com › home › java › java array › removing an element from an array in java
Removing an Element from an Array in Java | Baeldung
June 12, 2024 - Given the array below, let’s remove an element at index 2: A simple way of doing this would be to replace the value stored at index 2 with the value stored at index 3 until we reach the end of the array: Notice that by removing the element ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › remove/delete an element from an array in java
Remove/Delete An Element From An Array In Java
April 1, 2025 - To remove an element from an array, we first convert the array to an ArrayList and then use the ‘remove’ method of ArrayList to remove the element at a particular index. Once removed, we convert the ArrayList back to the array. The following implementation shows removing the element from an array using ArrayList. import java.util.*; import java.util.stream.*; class Main { public static int[] remove_Element(int[] myArray, int index) { if (myArray == null || index &lt; 0 || index &gt;= myArray.length) { System.out.println("non-existing index"); return myArray; } //array to arrayList List&lt;
🌐
Netjstech
netjstech.com › 2017 › 07 › how-to-remove-elements-from-array-java.html
How to Remove Elements From an Array Java Program | Tech Tutorials
Here the array removal is done ... enter the element to be removed. Search in the array for the given element. If found shift all the element after that index to the left by one element....
🌐
How to do in Java
howtodoinjava.com › home › java array › removing items from an array in java
Removing Items from an Array in Java
February 10, 2022 - Learn to remove the array items in Java by the index positions as well as the item values using ArrayUtils, Collections APIs and custom code.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-remove-methods-arraylist-remove
How To Use remove() Methods for Java List and ArrayList | DigitalOcean
September 9, 2025 - For details, see the Oracle Java SE Support Roadmap. There are two remove() methods to remove elements from the List. ... index): This method removes the element at the specified index and returns it. The subsequent elements are shifted to the left by one place.
🌐
Java67
java67.com › 2012 › 12 › how-to-remove-element-from-array-in-java-example.html
How to Remove an Element from Array in Java with Example | Java67
Here is a complete code example of how to remove an element from Array in Java. In this example, we have used a primitive array, particularly int array and Apache commons ArrayUtils to remove an integer based on its index. The ArrayUtils also provided several overloaded remove() methods for ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } }
🌐
Reddit
reddit.com › r/learnprogramming › having trouble with java arrays, removing an element then returning a new size of an array
r/learnprogramming on Reddit: Having trouble with java arrays, removing an element then returning a new size of an array
July 13, 2018 -

So guys i'm having trouble finding a solution for this question. I'm kinda frustrated that i didn't solve this kind of problem during my exam for employment.

Problem: The task is to provide an implementation for the given function. int[] removeElem(int[] array, value), the objective is to find if the array contains the given 'value' inside it. if found then remove the element from the array then return an array with a new size, else return original array.

So far this is what i got:

  public static void main(String[] args) {
  int[] myArray = {5,6,9,4};
  
  int[] newArray = removeElem(myArray,9);
  
  for(int i=0;i<newArray.length;i++){
      System.out.print(newArray[i] + " ");
  }

}

public static int[] removeElem(int[] array, int value){
    int[] tempArray = array;
    int newArrayLength = 0;
    int[] newArray;
    
    for(int index=0;index < tempArray.length; index++){
        if(tempArray[index] == value){
            tempArray[index] = -1;
            newArrayLength++;
        }
    }
    
    newArray = new int[tempArray.length - newArrayLength];
    for(int index=0;index < newArray.length; index++){
        if(tempArray[index] != -1){
            newArray[index] = tempArray[index];
        }
    }
    
    return newArray;
    
}

Unfortunately i can't get it right :/

Edit: Thank you! everyone for suggesting different solutions to this problem, I feel like I need to work more on my solutions and such :|.

Top answer
1 of 5
2
I’m on my phone so I’ll just explain what im thinking in pseudo code, but why don’t you just do something like: Create a new array of size n-1 where n is the size of the original array. Iterate over the old array in a for loop, adding each element to the new array. If the element at the ith position equals the element to remove, continue to the next iteration. Would that work? EDIT: I'm at my PC now so here's the code. I ended up doing it a little differently than I mentioned above. Note that it doesn't account for if the element appears multiple times in the array or when the element does not exist. public static void main(String []args) { int[] myArray = { 5,6,9,4 }; int[] newArray = removeElem(myArray, 9); for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i] + " "); } public static int[] removeElem(int[] array, int value) { int index = -1; // First confirm the element to remove exists in the array for (int i = 0; i < array.length; i++) { // If it does, store its index if (array[i] == value) index = i; } // If element exists if (index != -1) { int[] newArray = new int[array.length - 1]; for (int i = 0; i < array.length; i ++) { if (i < index) newArray[i] = array[i]; else if (i > index) newArray[i - 1] = array[i]; } return newArray; } // Do some error handling for if the element does not exist else return array; } Output is: 5 6 4
2 of 5
2
newArray[index] = tempArray[index]; This is your problem. As you skip elements that are to be removed, the insertion index in newArray must no longer match the read index in tempArray, but you're using the same index for both. You need to use two indices, and only increment the newArray index when you insert an element, while incrementing the tempArray index every time. Also the whole thing would be better if you just used Lists or Streams, but maybe your interviewer didn't want that. It's really best to try to avoid using raw arrays in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › removing-element-from-the-specified-index-in-java-arraylist
Removing Element from the Specified Index in Java ArrayList - GeeksforGeeks
March 31, 2023 - Size of list: 5 Flower ArrayList = [red-rose, tulip, sun-flower, marie-gold, orchid] Removing element at index = 2 After removing element Size of list: 4 Flower ArrayList = [red-rose, tulip, marie-gold, orchid]
🌐
Java Guides
javaguides.net › 2024 › 05 › java-remove-element-from-array.html
Java: Remove Element from Array
May 29, 2024 - The removeElement method finds the index of the element to be removed. If the element is not found, the original array is returned. Two calls to System.arraycopy are used to copy the parts of the array before and after the element to be removed ...