×

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used in C programming to hold values of basic data types like int, char, float, etc. We'll start by looking at how to delete or remove a certain element from an array in C.

The user must specify the point from where the array's element should be deleted to remove a specific element from an array. The element's deletion has no impact on the array's size. We must also establish whether it is feasible to delete data from an array.

Let's say, for instance, that an array has seven elements, arr[] = (12,27,16,4,10,15,5); and the user wants to delete an element 8. The user must first locate the 4th element, the 8th element, to decide whether or not the deletion is possible. No element should be higher than the total number of elements in an array. The user wishes to remove the element in the eighth place of the array, which is not allowed because there are 7 elements in it.

Steps to remove an element from an array

Step 1: Enter the array's size using the num variable, define the position using the position variable, and the counter value using the i variable.

Step 2: Insert elements into an array using a loop until i num is satisfied.

Step 3: At this stage, the user or programmer should provide the location of the precise element they wish to remove from an array.

Step 4: Compare an element's position (pos) to the total number of elements (num+1). The element cannot be deleted if the position is greater than num+1; move on to step 7 instead.

Step 5: Shift the remaining array members to the left while removing the specific element.

Step 6: After an element has been deleted or removed from an array, display the final array.

Step 7: Stop the programme or leave it.

Remove element from array Program

#include <stdio.h>
#include<conio.h>
int main()
{
int array[100]; //Array Initialization
int position, c, n;//declare the variables
printf("Enter number of elements in array\n");/* asks the user to specify the array's size */
scanf("%d", &n);
// put the array's elements in
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]); // making loop ends.
printf("Enter the location where you wish to delete element\n");
// seeks the user's position
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];//for loop ends
printf("Resultant array:\n");
//Displays array
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);//for loop ends
}
return 0;
}//main function ends

Output

Remove an element from an array in C

The array's size is unaffected by the deletion of an element. Additionally, the likelihood of deletion is examined.

A user-defined size array with a deleted element is provided.

Operations involved:

Step 1: Array input

Step 2: Element to be deleted

Step 3: Traversing the array till we reach the element

Step 4: Delete that element, and the resultant array size is reduced by one unit

For instance, if an array has five elements and you try to delete the element at position 6, it will not work.

Example

#include <stdio.h>
#include <conio.h>
int main()
{
int array[100]; // initialising an array.
int position, c, n; // variables must be declared
printf("Enter number of elements in array\n");
// enter the array's size
scanf("%d", &n);
printf("Enter %d elements\n", n);
// Put the array's components in.
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]); //for loop ends
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 ) 
printf("Deletion not possible.\n");
else
{ 
for ( c = position - 1 ; c < n - 1 ; c++ ) 
array[c] = array[c+1]; //for loop ends 
printf("Resultant array is\n");
//displays array
for( c = 0 ; c < n - 1 ; c++ ) 
printf("%d\n", array[c]); 
//for loop ends 
} 
return 0;
}//main function ends.

Output

The array's element count should be entered.
5
Enter 5 components.
4
12
22
34
45
When deleting an element, enter its desired position.
2
The resultant array is
4
22
34
45

Related Topics

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.