Joshua Lucero February 2016

Removing a specified value from array

I'm trying to create a function which will copy elements from one array to another array, unless a specified variable is mentioned, in which case it will not be copied over. The function will then output the new array without the specified variables included.

int *NewArray(int array[], int len, int num){
    int *array2 = new int[len];
    int temp;
    for(int i=0;i<len;i++){
        temp = array[i];
        if(temp != num){
            array2[i]=temp;
        }
        else{
            array2[i] = array[i+1];
        }
    }
    return array2;
}

Answers


R Sahu February 2016

The problem with your for loop is that it uses only one index to access both the arrays. You need two indices.

Say the item you want to remove is at index 1. After that, you need to make sure that

array2[i] = array[i+1];

Even though you have such a line, that is used only for the matching item. It does not use that logic for subsequent items.

This is what you need:

for(int i=0, j = 0; i<len; ++j ){
   if(num == array[j]){
      ++j;
      // Don't increment i, increment j
      // to skip the element from array
   }
   else {
      array2[i] = array[j];
      // Increment i since it is being assigned a value from array.
      ++i;
   }
}


101010 February 2016

You could use std::copy_if as below:

int *NewArray(int array[], int &len, int num) {
  int *array2 = new int[len];
  auto it = std::copy_if(array, array + len, array2, 
                         [&num](int const i){ return i != num; });
  len = std::distance(array2, it);

  return array2;
}

Live Demo


Goodies February 2016

Using something like std::vector would be beneficial in your case, but seeing as you may have a valid reason for using arrays, you are going about it in a more complex way than is needed. This is definitely more c-esque syntax, but I think it performs what you want it to do without really getting into any STL libraries.

#include <iostream>
#include <cstdlib>

int *NewArray(const int const array1[], const size_t len, const int num, size_t *newlen) {
    int *array2 = new int[len];
    size_t counter = 0;
    for (int i = 0; i < len; i++)
        if (array1[i] != num)
            array2[counter++] = array1[i];
    *newlen = counter;
    return array2;
}

int main(int argc, char **argv) {
    int arr1[] = { 1, 2, 3, 4, 5, 6, 7};
    size_t oldlen = sizeof arr1 / sizeof(int);
    size_t newlen;
    int *arr2 = NewArray(arr1, sizeof arr1 / sizeof(int), 3, &newlen);
    int i;
    for (i = 0; i < newlen; ++i)
        std::cout << arr2[i] << std::endl;
    delete arr2;
    system("pause");
}

Here's how I would do it with a vector.

std::vector<int> NewVector(const std::vector<int> const vec1, const int num) {
    std::vector<int> vec2 (vec1.size());
    auto it = std::copy_if(
        vec1.begin(),                       // where to start
        vec1.end(),                         // where to end
        vec2.begin(),                       // where to insert
        [&num](const int i) { return i != num; }    // lambda predicate
    );
    vec2.resize(std::distance(vec2.begin(), it));
    return vec2;
}

Post Status

Asked in February 2016
Viewed 3,112 times
Voted 7
Answered 3 times

Search




Leave an answer