Java Program to Delete a specific element from an Array
In this post, you will learn how to delete a specific element from an array using the Java programming language.
Algorithm
1. Start
2. Input an array arr[] of size n
3. Initialize the index to delete, Set indexToDelete to -1
4. Iterate through the array using a loop from i = 0 to i < n :
- If arr[i] == elementToDelete, then,
- Set indexToDelete = i.
- Break out of the loop (the element has been found).
- Print "Element not found in the array."
- Exit the algorithm.
7. Copy Elements Except the Deleted One:
- Initialize a variable k = 0 (index for newArray[]).
- Iterate through the original array arr[] using a loop from i = 0 to i < n
- If i == indexToDelete, skip this iteration
- Otherwise, copy arr[i] to newArray[k] and increment k.
Java Program
In the given Java program, we have declared an array. Then, we have asked the user to provide the specific element they wish to delete. The program locates the index of the element to delete. At last, we have created a new array by excluding the specified element.
import java.util.Arrays;
import java.util.Scanner;
public class DeleteSpecificElement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Define the array
int[] array = {11, 12, 14, 15, 16, 17, 18};
System.out.println("Original Array: " + Arrays.toString(array));
// Get the element to delete
System.out.print("Enter the element you want to delete: ");
int elementToDelete = scanner.nextInt();
// Find the index of the element to delete
int indexToDelete = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == elementToDelete) {
indexToDelete = i;
break;
}
}
// If the element is found
if (indexToDelete != -1) {
// Create a new array with a size less than the original array
int[] newArray = new int[array.length - 1];
// Copy elements except the one to delete
for (int i = 0, k = 0; i < array.length; i++) {
if (i == indexToDelete) {
continue; // Skip the element to delete
}
newArray[k++] = array[i];
}
// Print the new array
System.out.println("Array after deletion: " + Arrays.toString(newArray));
} else {
System.out.println("Element not found in the array.");
}
scanner.close();
}
}
Output of the above code:
Original Array: [11, 12, 14, 15, 16, 17, 18]
Enter the element you want to delete: 11
Array after deletion: [12, 14, 15, 16, 17, 18]
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number