You could use commons lang's ArrayUtils.

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

Answer from Peter Lawrey on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-array-elements
Java Remove Array Elements: Methods and Examples | DigitalOcean
May 2, 2025 - The code removes the element at index 3. This method simply copies all the elements except the one at index 3 to a new array. Unlike the previous case, this code will delete the element based on its value. This will not work with duplicates since the size of the array after deletion has to be known. package com.journaldev.java; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = new int[]{1,2,3,4,5}; int[] arr_new = new int[arr.length-1]; int j=3; for(int i=0, k=0;i<arr.length;i++){ if(arr[i]!=j){ arr_new[k]=arr[i]; k++; } } System.out.println("Before deletion :" + Arrays.toString(arr)); System.out.println("After deletion :" + Arrays.toString(arr_new)); } }
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
[Java] Inserting and deleting elements in an array
Can you tell us a little more about the concept you're not understanding ? (Creating an array? Inserting while looping? Maybe pass-by-reference or pass-by-value ?) edit: Happy cakeday mick5000x ! More on reddit.com
🌐 r/learnprogramming
28
11
January 11, 2013
Java - Remove element from array?
I want to remove the dog.. How can I do this? Edit: I am aware that ArrayLists are recommended, I'd love to use it but I'm not allowed to :( Archived post. New comments cannot be posted and votes cannot be cast. ... Create your account and connect with a world of communities. ... By continuing, ... More on reddit.com
🌐 r/AskProgramming
1
1
January 6, 2021
How to remove numbers from an array? (java)
There are a couple of different ways that you could go about doing this. If the array is an actual array and not some other List-based array, you won't be able to straight up remove the number from the list because that will leave an empty hole in your array. You are trying to solve the problem by removing the duplicates, but I would probably recommend trying to solve the problem by creating a new array of just the unique numbers. The approach I would be to create a new variable that stores an ArrayList. Then you can loop through each element in your array. Each time you iterate through the loop, you can use ArrayList's builtin contains() method to check if the number is already in the ArrayList. If it is not, you can use the add() method to add that to the new array. After you've looped through each element in your original array, you should have a new second array with no duplicate values. If you can't keep your answer as an ArrayList and it must be an array, you can use ArrayList's toArray() method to convert it back into an array. There are other (probably more efficient) ways to solve this problem as well, but this is the first and probably easiest approach that comes to mind. More on reddit.com
🌐 r/learnprogramming
2
1
January 23, 2021
🌐
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 - Arrays have a fixed size, so creating a new array without the target element is often necessary. ... The basic approach to remove an element at a specific index is using a loop. ... // Java program to remove an element // from a specific index ...
🌐
CodeProject
codeproject.com › Questions › 5368786 › Remove-element-from-array-in-java
https://www.codeproject.com/Questions/5368786/Remo...
Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
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 - In this quick tutorial, we’ll ... array in Java. 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 ...
🌐
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); } }
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › arraylist › .remove()
Java | ArrayList | .remove() | Codecademy
March 21, 2022 - Beginner Friendly.Beginner Friendly17 hours17 hours · An element can be removed from an ArrayList instance by being passed to the .remove() method. It can be referenced either by value ...
🌐
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 by the item values.
🌐
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
That's all on How to remove an element from Array in Java. You can create your own method to delete objects from Array as well but ArrayUtils is tried and tested by the Java community and offer a convenient way to delete an element from Array in Java.
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - To remove this element, we simply "shift" all elements after it. This means that we're going to iterate through all the elements after 40 and simply "move" them one place to the left. ... Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, ...
🌐
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 - Using Java8 streams, we can delete an element from an array. In order to do this, first, the array is converted to a stream. Then the element at the specified index is deleted using the filter method of streams.
🌐
Netjstech
netjstech.com › 2017 › 07 › how-to-remove-elements-from-array-java.html
How to Remove Elements From an Array Java Program | Tech Tutorials
When you remove an element from an array, you can fill the empty space with 0, space or null depending on whether it is a primitive array, string array or an Object array. Other alternative is to create a new array and copy the elements in that array. New array should have size of old array’s ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › remove element(s) from arraylist in java
Remove Element(s) from ArrayList in Java
August 7, 2023 - ArrayList<String> arraylist2 = new ArrayList<>(); //1 - Remove an element from the specified index position arraylist.remove(indexPosition); //2 - Remove the first occurence of element by its value arraylist.remove(element); //3 - Remove all elements of the specified collection from arraylist arraylist.removeAll(Arrays.asList(ele1, ele2, ele3)); //4 - Remove all elements matching a condition arraylist.removeIf(e -> e.contains("temp"));
🌐
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.
🌐
w3resource
w3resource.com › java-exercises › array › java-array-exercise-7.php
Java - Remove a specific element from an array
May 9, 2025 - // Import the Arrays class from ... = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49}; // Print the original array using Arrays.toString() method....
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - Now we have seen removing elements in an ArrayList via indexes above, now let us see that the passed parameter is considered an index. How to remove elements by value. Method 2: Using remove() method by values · Example: Java ·
🌐
Medium
medium.com › @Roshan-jha › solving-the-remove-element-problem-in-java-1a9333a55193
Solving the “Remove Element” Problem in Java | by Roshan Jha | Medium
November 18, 2023 - One pointer is used to iterate through the array, while the other pointer keeps track of the current position where elements not equal to the specified value should be placed. Let’s take a look at the Java code to solve this problem: public class RemoveElement { public static int removeElement(int[] nums, int val) { int k = 0; // Initialize the variable to count elements not equal to val // Iterate through the array for (int i = 0; i < nums.length; i++) { // If the current element is not equal to val if (nums[i] != val) { // Move the element to the front of the array nums[k] = nums[i]; // In
🌐
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 ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Remove element from an Array Java - Examples Java Code Geeks - 2026
July 6, 2022 - If everything goes well, the element present at index=2 will be removed from the specified array.To find out more about the best way to copy an array for each possible case you can check the Java Copy Array Example