Clearing an array of defined element(s) - page 5

 
Konstantin Nikitin:
Purely about the task at hand. Remove value and empty

add a function so you can filter by criterion and you'll be good to go. You could even add it to your collection :-)

something like

int arrayFilter(T &data[], const T value, bool (*cmp)(const T&,const T&)=NotEqual<T>)

to pull out all values greater than 3: arrayFilter<long>(array,3,GreaterThen<long>).

I'm not sure that MQL templates will allow to do it easily

 
TheXpert:

I'm not imposing.

Thank you, you are kind today :-)

About the task. Imho it is more convenient to operate with a type from data collection likeCArrayDouble. My example:

//--- https://www.mql5.com/ru/docs/standardlibrary/datastructures/carraydouble
#include <Arrays\ArrayDouble.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double delta=1 e-3;
   CArrayDouble ArrDouble;
   ArrDouble.Delta(delta);
   double src_arr[]={1,2,3,6,9,5,6,3,25,6,8,7,4};
   double value_to_delete=3.0;
   if(ArrDouble.AddArray(src_arr))
     {
      ArrDouble.Sort();
      if(ArrDouble.IsSorted())
        {
         int first_idx,last_idx;
         first_idx=ArrDouble.SearchFirst(value_to_delete);
         last_idx=ArrDouble.SearchLast(value_to_delete);
         if(first_idx>=0)
           {
            PrintFormat("\nЭлементов до удаления: %d",ArrDouble.Total());
            if(ArrDouble.DeleteRange(first_idx,last_idx))
                  PrintFormat("Удалено элементов: %d",last_idx-first_idx+1);             
            PrintFormat("Элементов после удаления: %d",ArrDouble.Total());         
           }
        }
     }
  }
//+------------------------------------------------------------------+

There is a drawback though. The output will be a sorted array.

 
Denis Kirichenko:
Ohhh, sometimes it's better to be silent and look like a fool than...
 
TheXpert:
Ohhh, sometimes it's better to be silent and look like a fool than...

than to say anything and clear up any doubts....

 
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 ?

By looping through the array and removing unneeded values, you overwrite the value in the next cell in the place of the removed one. Shift to the left all values following the erased value and count the number of erased values in the counter.

Then do ArrayResize() taking into account the number of erased values.

Zy. Do not move the values you want to erase. You can shift to the left all values except the ones you erase.

The shift of values to the left should be equal to the number of erased cells.

 
Vladimir Pastushak:

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.

You have been asking similar questions for several years now. Have you learned much? Sorry, but you seem to have remained at the level of bytes and elementary arrays.

The question itself is formulated incorrectly. The task is not to remove repeating values (task of GCE level) but in something much bigger, you must update the list of valid items. If so, the question should sound completely different. You confuse and mislead the participants and, first of all, yourself: impose on the participants the wrong solution in principle and ask to make it effective.

 
Реter Konow:

By looping through the array and removing unwanted values from it, overwrite the value in the next cell in place of the removed value. Shift to the left all values following the erased value and count the number of erased values in the counter.

Then do ArrayResize() taking into account the number of erased values.

Zy. Do not move the values you want to erase. You can shift to the left all values except the ones you erase.

The shift of values to the left should be equal to the number of erased cells.

 int Arr[20] = 1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2;
 int deleted;
//-------------- 
for(int a1 = 0; a1 < ArraySize(Arr); a1++)
  {
   if(deleted)Arr[a1] = Arr[a1 + deleted];
   
   if(Arr[a1] == 3)deleted++; 
  }
//-------------- 
ArrayResize(Arr, ArraySize(Arr) - deleted);

Check this option.

 

Sorry. It looks like the lines need to be reversed:


 int Arr[20] = 1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2;
 int deleted;
//-------------- 
for(int a1 = 0; a1 < ArraySize(Arr); a1++)
  {
   if(Arr[a1] == 3)deleted++; 

   if(deleted)Arr[a1] = Arr[a1 + deleted];
  }
//-------------- 
ArrayResize(Arr, ArraySize(Arr) - deleted);
 
Реter Konow:

Sorry. It looks like the lines need to be reversed:

Riddle: How many apples will Pinocchio have if Pinocchio gives him two apples and Papa Carlo gives him three?
Answer: Ten. The source variables need to be initialized.

 
Реter Konow:

Sorry. It looks like the lines need to be reversed:


More work to be done:

int Arr[20] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2};
int deleted;
//-------------- 
for(int a1 = 0; a1 < ArraySize(Arr); a1++)
  {
   if(Arr[a1] == 3)
     {
      if(deleted)a1-= deleted;//Cмещение назад.
      deleted++; 
     }
   if(deleted)Arr[a1] = Arr[a1 + deleted];
  }
//-------------- 
ArrayResize(Arr, ArraySize(Arr) - deleted);
Reason: