Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 144

 
Vitaly Muzichenko:

I'm already confused)

You have an array:

Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};

What should the function eventually return and why? The number of matches for each number, or a specific maximum number, or ... Did I write something, but it must be wrong, and wrong?

I don't know how to explain...

We see an array:1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3,
We mark identical numbers with the same colour (if there is only one number, there is no duplicate for it - it is not needed):
1, 2, 3, 1, 2, 1 , 2 , 2, 1, 1, 1, 3, 3, 3 , 3 , 3 , 3 , 3, 3 , 3, 3,3
Count the number of numbers with different colours: 3 - this is the result.

 
fxsaber:
You have checked the old code. Double-check it.
Here... I'm kind of weird... ...and then I got it from somewhere else.
 
Artyom Trishkin:

I don't know how to explain...

We see an array:1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3,
We mark identical numbers with the same colour (if there is only one number, there is no double for it - it is not needed):
1, 2, 3, 1, 2, 1 , 2 , 2, 1, 1, 1, 3 , 3, 3 , 3 , 3 , 3, 3 , 3 , 3, 3,3
Count the number of numbers with different colours: 3 - this is the result.

Maybe)

void OnTick()
{
int Arr[]={1, 2, 3, 1, 2, 1, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
Comment( GetM(Arr) );
}

int GetM(int &Mas[])
{
int MasTemp[][2]; // Временный массив
int c=0,t=0;

ArraySort(Mas);
int ArrSize= ArraySize(Mas);
  for(int i=0; i<ArrSize; i++) {
    for(int x=i; x<ArrSize; x++) {
     if(Mas[i]==Mas[ArrayMinimum(Mas,WHOLE_ARRAY,x)]) {
      c++;
     }
    }
  
   if(c>0) {
     t++;
     ArrayResize(MasTemp,t);
     MasTemp[t-1][0]= c;
     MasTemp[t-1][1]= Mas[i];
   }
   c=0;
  }
  
  int ArrRange=ArrayRange(MasTemp,0);
  if(ArrRange>0) {
    ArraySort(MasTemp);
  // Comment("Цифра: ",MasTemp[ArrRange-1][1],", Количество: ",MasTemp[ArrRange-1][0]);
    return( MasTemp[ArrRange-1][1] );
  }
  
  return(-1);
}
 
Artyom Trishkin:
There ... I'm a weirdo... I thought it came from there, but it came from someplace else.
Generalized
// Возвращает количество различных элементов массива, которых не меньше Repeat
template <typename T>
int Strange( const T &InArray[], const int Repeat = 2 )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    for (int i = 1; i < Size; i++, Tmp++)
      if (Array[i - 1] != Array[i]) // если будут структуры, то есть более универсальная запись
      {
        if (Tmp >= Repeat)
          Res++;
        
        Tmp = 0;
      }
      
    if (Tmp >= Repeat)    
      Res++;
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  
  for (int i = 1; i <= 4; i++)
    Print(Strange(Array, i));
}
 
fxsaber:
Summarised
///
Thank you. All super - I'd spend a day...
 
Vitaly Muzichenko:

Maybe)


Thank you, it's still easier without array flipping ;)
 
fxsaber:
Summarised by
// если будут структуры, то есть более универсальная запись
Now that's interesting.
 
Artyom Trishkin:
This is interesting
      if (_R(Array[i - 1]) != Array[i]) // TypeToBytes.mqh

With this notation, an Array can not only be a numeric type, but any simple structure.

Another thing is that ArraySort for structures, of course, does not work.

 
fxsaber:
      if (_R(Array[i - 1]) != Array[i]) // TypeToBytes.mqh

With this notation, an Array can not only be a numeric type, but any simple structure.

Another thing is that ArraySort for structures, of course, does not work.

That's right... But structures are usually complex. And they still have to be sorted...
 
Artyom Trishkin:
Exactly... But structures are usually very complex. And they still have to be sorted...

Complex are containing objects (strings, for example).

MqlTick is a simple structure.

MqlTradeRequest - complex.

ArraySort can easily be written for simple structures.