Description
To delete element in a array sorted in ascending order, first find the position of that element. Then follow the normal process of element deletion.
C/C++
/* C program to delete element in an array sorted in ascending order*/ //Save it as DeleteElementSortedArray.c #include<stdio.h> int main(){ int i, n, pos, num; printf("Enter the size of array : "); scanf("%d",&n); int arr[n]; printf("Enter the elements of the array : "); for (i = 0; i < n; i++) { scanf("%d",&arr[i]); } printf("Enter the element to be deleted : "); scanf("%d",&num); /*First check element is prsent or not in the array, if it is not present print element is not present. If it is present return position of that element*/ pos = -1; for (i = 0; i < n; i++) { if (arr[i] == num) { pos = i; break; } } if (pos == -1) { printf("%d is not present",num); } else { printf("%d is present at position %d to be deleted",num,pos); /* Execute a loop to move all elements left by 1 position having index greater than position where to delete element */ for (i = pos; i < n-1; i++) { arr[i] = arr[i + 1]; } // Finally print new array after deletion of element printf("\nThe new array is : "); for (i = 0; i < n - 1; i++) { printf("%d ", arr[i]); } } return 0; }
Input: Enter the size of array : 5 Enter the elements of the array : 3 6 9 78 89 Enter the element to be deleted : 9 Output: 9 is present at 2 to be deleted The new array is : 3 6 78 89
Java
/* Java program to delete element in an array sorted in ascending order*/ //Save it as DeleteElementSortedArray.java import java.io.*; import java.util.Scanner; public class DeleteElementSortedArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the size of array : "); int n = scanner.nextInt(); int arr[] = new int[n]; System.out.println("Enter the elements of the array : "); for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } System.out.println("Enter the element to be deleted : "); int num = scanner.nextInt(); /*First check element is prsent or not in the array, if it is not present print element is not present. If it is present return position of that element*/ int pos = -1; for (int i = 0; i < n; i++) { if (arr[i] == num) { pos = i; break; } } if (pos == -1) { System.out.println(num + " is not present"); } else { System.out.println(num + " is present at position" + pos + " to be deleted"); /* Execute a loop to move all elements left by 1 position having index greater than position where to delete element */ for (int i = pos; i < n-1; i++) { arr[i] = arr[i + 1]; } // Finally print new array after deletion of element System.out.println("\nThe new array is : "); for (int i = 0; i < n - 1; i++) { System.out.print(arr[i] + " "); } } } }
Input: Enter the size of array : 5 Enter the elements of the array : 3 6 9 78 89 Enter the element to be deleted : 9 Output: 9 is present at 2 to be deleted The new array is : 3 6 78 89
Related programs
1) Insert a number in an array sorted in ascending order2) Program to insert a number at given position of an Array
3) Program to delete an element from given location in an array
4) Program to merge two sorted arrays
5) Program to swap maximum and minimum element of Array
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find the smallest missing number
8) Program to Find K largest elements from array
9) Program to make pair of elements alternatively
10) Remove Duplicate Elements From Array