Delete an element from array using two traversals and one traversal in C++ program


In this tutorial, we are going to learn how to delete an element with two loops and on loop. We don't need to delete the element. We will just replace the deleting element with the next elements.

Two Traversals

Let's see the steps to delete an element from the array using two loops.

  • Initialize the array and delete the element.

  • Write a function to delete the element.

    • Iterate over the array and search for the element.

    • If the element found, break the loop.

    • If the element is found, decrease the size of the array.

    • Move all the elements to their previous index.

    • Return the new size of the array.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int searchAndDeleteElement(int arr[], int n, int k) {
   int i;
   // searching for the element
   for (i = 0; i < n; i++) {
      if (arr[i] == k) {
         break;
      }
   }
   // if the element is present
   if (i < n) {
      // moving all the elements to previous index after k
      n = n - 1;
      for (int j = i; j < n; j++) {
         arr[j] = arr[j+1];
      }
   }
   // returning updated index
   return n;
}
int main() {
   int n = 6, k = 4;
   int arr[] = {1, 2, 3, 4, 5, 6};
   int updatedLength = searchAndDeleteElement(arr, n, k);
   // printing the array
   for (int i = 0; i < updatedLength; i++) {
      cout << arr[i] << " ";
   }
   cout << endl;
   return 0;
}

Output

If you execute the above program, then you will get the following result.

1 2 3 5 6

One Traversal

Let's see the steps to delete an element from the array using one loop.

  • Initialize the array and deleting the element.

  • Write a function to delete the element.

    • Iterate over the array and search for the element.

    • If the element is found, skip the statement.

    • Move all the elements to their previous index.

    • If the element is found, then return n - 1 else return n.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int searchAndDeleteElement(int arr[], int n, int k) {
   // checking for the last element
   if (arr[n-1] == k) {
      return n - 1;
   }
   bool isElementFound = false;
   for (int i = 0; i < n; i++) {
      // checking for k
      if (arr[i] == k && !isElementFound) {
         isElementFound = true;
         continue;
      }
      // if the element is already found move all the element to their previous indexes
      if (isElementFound) {
         arr[i-1] = arr[i];
      }
   }
   // returning updated n
   if (isElementFound) {
      return n - 1;
   }
   return n;
}
int main() {
   int n = 6, k = 4;
   int arr[] = {1, 2, 3, 4, 5, 6};
   int updatedLength = searchAndDeleteElement(arr, n, k);
   // printing the array
   for (int i = 0; i < updatedLength; i++) {
      cout << arr[i] << " ";
   }
   cout << endl;
   return 0;
}

Output

If you run the above code, you will get the following result.

1 2 3 5 6

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 27-Jan-2021

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements