Delete element from any position in array

Back to Programming


The elements of the array are taken as input. Then the index of the element which is to be deleted is taken as the input also. In the case of an array, the element cannot be deleted, so the concept is to shift all the elements one index left starting from the next element of the given position.



INPUT: Array elements
OUTPUT: the new array after deleting the element
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
	Read pos [index of which the element has to be deleted (index starting from 0)]
Step 2: [Deleting the element]
	For i=pos+1 to n-1 repeat
		Set a[i-1]<-a[i]
	[End of ‘for’ loop]
Step 3: [printing the array after deletion]
	Print “The array after deletion: “
	For i=0 to n-2 repeat
		Print a[i]
	[End of ‘for’ loop]
Step 4: Stop.



The time complexity to take the number of elements is O(n) and the time complexity to delete the element is also O(n-pos).