Clearing an array of defined element(s)

 

There is an array containing a set of data of type 1,2,3,6,9,5,6,3,25,6,8,7,4 you need to remove e.g. values 3 and get the same array without 3 and empty spaces in the output...

I'm looking for the fastest way to clear an array of unnecessary values...

The following example comes to mind

int ArrayDeleteEmpty(int &one[])
  {
   int two[];   int c=0;
   for(int i=0; i<ArraySize(one); i++)
      if(one[i]!=NULL && one[i]!="")
        {
         c++;
         if(ArrayResize(two,c,10000)>=0)
            two[c-1]=one[i];
        }
   for(int i=0; i<ArraySize(two); i++)
      if(ArrayResize(one,i+1,10000)>=0)
         one[i]=two[i];
   return c;
  }

Maybe there is a cheaper and faster way ?

 
Vladimir Pastushak:

There is an array containing a set of data of type 1,2,3,6,9,5,6,3,25,6,8,7,4 you need to remove e.g. values 3 and get the same array without 3 and empty spaces in the output...

I'm looking for the fastest way to clear an array of unnecessary values...

The following example comes to mind

Maybe there is a more economical and faster way ?

ArrayResize should be removed from loops, and (if struggling for every LONG, check conditions and single Resize at the end)

But it's kind of typical to take everything out of loops, which can be done later in bulk or not to do at all...

 

1. Rewrite array one[] into array work[] of the same dimension, removing unnecessary elements and shifting the required elements in their place.

2. Rewrite array work[] into array two[] of the new dimension.

HH: Remove function call from loop operator(ArraySize).
 
int ArrayDeleteEmpty(const int del, int &one[])
  {
   int i = ArrayBsearch(one, del),
       y = ArraySize(one)-1;
   
   if(one[i] != del)
     return 0;
   
   for(; i<y; i++)
      one[i] = one[i+1];
   ArrayResize(one, y);
   
   return y;
  }
 

Here's a variant like this:

template<typename T>
int arrayFilter(T &data[], const T value)
{
  int dst = 0, src = 0;
  for(; src < ArraySize(data); src++)
  {
    if(data[src] != value)
    {
      if(dst != src)
      {
        data[dst] = data[src];
      }
      dst++;
    }
  }
  
  if(dst < src)
  {
    ArrayResize(data, dst); // will only work with dynamic array
  }
  return dst;
}

void OnStart()
{
  long array[] = {1, 2, 3, 6, 9, 5, 6, 3, 25, 6, 8, 7, 4};
  ArrayPrint(array, 0, NULL, 0, arrayFilter<long>(array, 3));
}

Returns the number of elements in the filtered array. Can automatically reduce its size if it is dynamic.

 
Stanislav Korotky:

Here's a variant like this:

Returns the number of elements in the filtered array. Can automatically reduce its size if it's dynamic.

A few more (a few more diameters of ground) and we'll get to functionality :-)

set target [ filter $source [ lambda x { expr $x !=3 } ]

it's tcl, it's even shorter in lisp

 
The fastest way would be with ArrayCopy() and loop through in reverse order, so as not to move those elements that will be deleted.
 
Dmitry Fedoseev:
The fastest way would be with ArrayCopy() and loop through in reverse order, so as not to move those elements that will be deleted.

is not suitable, as there may be an array of structures containing strings.

 
One has 42 products, one has 6, one has 30. I'm afraid to imagine what an <x?!> it is inside with all those questions and answers
 
TheXpert:
One has 42 products, another has 6, another has 30. I'm afraid to imagine what an asshole there is inside with such questions and answers

A silly question is an unasked question.

I have several solutions to the problem, but I keep looking for better solutions and learning from others.

 
Thank goodness I only have one :) ... product and one solution option.