Program to Delete Element From an Array by Value
- Write a program to Delete Element From an Array by Value in C
- Write a program to Delete Element From an Array by Value in C++
- Write a program to Delete Element From an Array by Value in Python
- Write a program to Delete Element From an Array by Value in PHP
- Write a program to Delete Element From an Array by Value in Java
- Write a program to Delete Element From an Array by Value in Java Script
- Write a program to Delete Element From an Array by Value in C#
Explanation:
The following reasoning can be used to remove an element from an array based on its value:
Logic:
- Create a New Array (or Update In-Place):
- If you want to keep the original array intact, create a new array to store the result.
- If in-place modification is allowed, overwrite the array.
- Iterate Through the Array:
- For each element in the array:
- If the element matches the given value, skip it.
- Otherwise, add it to the new array (or overwrite in the same array).
- For each element in the array:
- Output:
- Return or print the modified array.
Program to Delete Element From an Array by Value
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5}; // Example array
int size = sizeof(arr) / sizeof(arr[0]);
int value = 3; // Element to delete
int i, j;
// Loop through the array to find the value and delete it
for (i = 0; i < size; i++) {
if (arr[i] == value) {
// Shift elements left to delete the element
for (j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size--; // Decrease the size of the array
i--; // Check the element that moved into current position
}
}
// Print the array after deletion
printf("Array after deletion: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-cpp">#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5}; // Example array
int size = sizeof(arr) / sizeof(arr[0]);
int value = 3; // Element to delete
int i, j;
for (i = 0; i < size; i++) {
if (arr[i] == value) {
for (j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size--;
i--;
}
}
cout << "Array after deletion: ";
for (i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
arr = [1, 2, 3, 4, 5] # Example array
value = 3 # Element to delete
if value in arr:
arr.remove(value) # Remove the first occurrence of the value
print("Array after deletion:", arr)
<?php
$arr = array(1, 2, 3, 4, 5); // Example array
$value = 3; // Element to delete
// Find the index of the element and remove it
$key = array_search($value, $arr);
if ($key !== false) {
unset($arr[$key]); // Remove the element
$arr = array_values($arr); // Re-index the array after removal
}
echo "Array after deletion: ";
print_r($arr);
?>
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 5}; // Example array
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr)); // Convert array to ArrayList
Integer value = 3; // Element to delete
list.remove(value); // Remove the first occurrence of the value
System.out.print("Array after deletion: ");
for (int num : list) {
System.out.print(num + " ");
}
System.out.println();
}
}
let arr = [1, 2, 3, 4, 5]; // Example array
let value = 3; // Element to delete
let index = arr.indexOf(value);
if (index !== -1) {
arr.splice(index, 1); // Remove element at the found index
}
console.log("Array after deletion:", arr);
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = {1, 2, 3, 4, 5}; // Example array
int value = 3; // Element to delete
// Use LINQ to remove the value (creates a new array without the element)
arr = arr.Where(x => x != value).ToArray();
Console.Write("Array after deletion: ");
foreach (var num in arr) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}