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.
Discussions

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
How can i remove an element from array in Java - Stack Overflow
Hello everyone i am trying to remove an name that the user has put in from an String Array, i am new to programming and i have tried this but it doesn't work. Can someone help me or tell me what i am More on stackoverflow.com
๐ŸŒ stackoverflow.com
Looking for a optimal way to delete element from object[] java
You should explain a bit more context: ... do you delete an item from the array vs how many times do you access it. Do you access elements by index, binary search, .. etc. ... Use an ArrayList. ... Create a new array of size n-1. Copy 0..index-1 elements from the old array to the new array. Copy index+1..n elements from the old array to the new array. Use java.lang.Syst... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Removing an item from an array list
The problem is in this part: for (Student i : roster) { int sId = i.getId(); if (id == sId) { roster.remove(id); } else if (id != sId) { System.out.println("ERROR: Invalid student ID.\n"); } } You can't remove items from the list while you're looping through it with this for loop. If you want to use a loop, you should use this kind of loop: for (int i = 0; i < roster.size(); ++i) { Student s = roster.get(i); int sId = s.getId(); if (id == sId) { roster.remove(i); i--; // This is important! } } See, this lets you remove the student at the current index. However, as you remove it, you have to decrement the index, otherwise you'll skip the next item in the list. More on reddit.com
๐ŸŒ r/learnjava
8
5
March 31, 2017
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-remove-array-elements
How to Remove Array Elements in Java | DigitalOcean
May 2, 2025 - In this tutorial, we explored various methods for removing elements from arrays in Java, including using for loops, System.arraycopy(), and converting to an ArrayList. We also highlighted the advantages of using ArrayLists for frequent element deletion or addition due to their dynamic nature ...
๐ŸŒ
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.
๐ŸŒ
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 - 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 in the above manner, the size of the array would remain the ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_remove.asp
Java ArrayList remove() Method
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. 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); } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ java_data_structures โ€บ remove elements from array in java
Remove Elements from Array in Java
September 1, 2008 - Enter the number of elements needed : 5 Enter the elements :: 44 55 62 45 55 Enter the position to delete the element : 3 Contents of the array after deletion :: 44 55 62 55
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - Due to the nature of array's memory placement, it is simply impossible to remove the element directly. Instead, to "remove" any element, all subsequent elements need to be shifted backward by one place. This will create an illusion that a specific element was removed.
๐ŸŒ
Codidact
software.codidact.com โ€บ posts โ€บ 289778
How do I remove an element from a Java array? - Software Development
// remove the element of the array at index i int i = 0; for (int j = i; j < N - 1; j++) { A[j] = A[j + 1]; } N -= 1; This is roughly what java.util.ArrayList does underneath for its remove(int) method.
๐ŸŒ
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 - Java arrays do not provide a direct remove method to remove an element. In fact, we have already discussed that arrays in Java are static so the size of the arrays cannot change once they are instantiated.
๐ŸŒ
Quora
quora.com โ€บ How-does-one-remove-an-element-from-an-array-in-Java
How does one remove an element from an array in Java? - Quora
Answer (1 of 10): Removing an element from an array is a cumbersome effort if the order matters in your array. Otherwise, it is really easy. Let me explain both ways. Order of the elements does NOT matter Letโ€™s assume your have a partially filled array [5, 9, 6, 8, 0, 0, 0] (0 means donโ€™t ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ remove-all-occurrences-of-an-element-from-array-in-java
Remove all Occurrences of an Element from Array in Java - GeeksforGeeks
July 11, 2025 - Filter all elements of the list which is equal to a given key ยท Convert the list back to an array and return it. Illustration: Here, we will use the Java 8 streams to remove all occurrences of an element from an array.
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ array โ€บ java-array-exercise-7.php
Java - Remove a specific element from an array
May 9, 2025 - Write a Java program to remove a specific element from an array. ... // Import the Arrays class from the java.util package. import java.util.Arrays; // Define a class named Exercise7. public class Exercise7 { // The main method where the program execution starts. public static void main(String[] args) { // Declare and initialize an integer array 'my_array'. int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49}; // Print the original array using Arrays.toString() method.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ remove-element
Remove Element - LeetCode
Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-remove-one-element-from-an-array
How to remove one element from an array - Quora
Answer (1 of 11): you are not explaining in what language. so will go to the basic of what an array is. an array is simple a repetition of a structure of data. all in memory concatenated. so, to actually remove one item the only way you can do it is, in a โ€œlogicalโ€ way or. you need to create ...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ arraylist โ€บ .remove()
Java | ArrayList | .remove() | Codecademy
March 21, 2022 - The .remove() method is used for removing specified elements from instances of the ArrayList class. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
๐ŸŒ
Studytonight
studytonight.com โ€บ java-programs โ€บ java-programt-to-delete-the-specified-integer-from-an-array
Java Programt To Delete the Specified Integer From an Array - Studytonight
March 5, 2021 - If the element is found, then start shifting the elements after that index to the left by one element. Now, print the updated array. ... Below is the code for the same. The below program demonstrates how to delete a specific element from an array by traversing through all the elements. /*Java Program to delete an element from an Array*/ import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n; //Array Size Declaration System.out.println("Enter the number of elements :"); n=sc.nextInt(); //Array Size