Thread: How to delete element of array without changing index order.

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    5

    How to delete element of array without changing index order.

    Hello everyone!

    I need to know how to delete value of element of an array without changing indices order. For example, there is 4 elements in my array and I want to delete second element, but others will stay in the same order. How can I do that? I would appreciate your help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    Three options come to mind:
    • Copy the subsequent elements to overwrite the deleted element, reducing the recorded size by 1. This maintains the relative order, but unfortunately deletions then run in linear time on average.
    • Designate a special "deleted" value and set the deleted element to that value. Unfortunately, it is not always feasible to designate such a special value.
    • Store pointers instead, then set the deleted element to be a null pointer. You still need to store the actual values somewhere though, e.g., by using malloc or a second array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    10
    Code:
    int arr[] = {1, 2, 3, 4};
    arr[1] = 0; //2nd element deleted
    After that, each element stays in the same order. The only difference is the second element is 0.
    If what you mean is to re-arrange the array so that there is no empty space between the elements, you can go through the array and move each element one by one, or use memmove to do it.
    Code:
    const int arr_sz = 4;
    for (int i = 0; i < arr_sz - 1; ++i)
    {   if (arr[i] == 0)
            arr[i] = arr[i + 1], arr[i + 1] = 0;
    }
    Or a simpler, faster way:
    Code:
    memmove(arr+1, arr+2, (arr+arr_sz) - (arr+2) );

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Quote Originally Posted by Exosomes View Post
    Code:
    int arr[] = {1, 2, 3, 4};
    arr[1] = 0; //2nd element deleted
    After that, each element stays in the same order. The only difference is the second element is 0.
    If what you mean is to re-arrange the array so that there is no empty space between the elements, you can go through the array and move each element one by one, or use memmove to do it.
    Code:
    const int arr_sz = 4;
    for (int i = 0; i < arr_sz - 1; ++i)
    {   if (arr[i] == 0)
            arr[i] = arr[i + 1], arr[i + 1] = 0;
    }
    Or a simpler, faster way:
    Code:
    memmove(arr+1, arr+2, (arr+arr_sz) - (arr+2) );
    Okay, got it. But what if I want to add a new a element and assign it to the deleted one so it would look like this: (1, 5, 3, 4).
    Actually, I am trying to do this with array of structure, but I do not think it will make a big difference.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Three options come to mind:
    • Copy the subsequent elements to overwrite the deleted element, reducing the recorded size by 1. This maintains the relative order, but unfortunately deletions then run in linear time on average.
    • Designate a special "deleted" value and set the deleted element to that value. Unfortunately, it is not always feasible to designate such a special value.
    • Store pointers instead, then set the deleted element to be a null pointer. You still need to store the actual values somewhere though, e.g., by using malloc or a second array.
    Hmm, could you explain third variant a bit more, please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print first element of array without using index
    By Andrijana in forum C Programming
    Replies: 5
    Last Post: 02-10-2020, 06:54 AM
  2. Array Element Insertion in Ascending Order Question
    By Omegalis in forum C Programming
    Replies: 1
    Last Post: 03-20-2018, 10:14 AM
  3. Replies: 4
    Last Post: 03-17-2013, 01:33 AM
  4. delete index from array
    By mrsirpoopsalot in forum C++ Programming
    Replies: 0
    Last Post: 04-03-2009, 10:17 PM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM

Tags for this Thread