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

 
Artyom Trishkin:

Nope :)))

1,1,1,2,3,3,2,1,4,4,5

is the number of numbers of the same colour.

Alexey Kozitsyn meant: in a sorted array.Maximum number of identical values in a sequence?

 
Mislaid:
Array:

1,1,1,2,3,3,2,1,4,4,5

Sorting:
1,1,1,1, 1,2, 2, 3,3,4,4,5

The output is 4 matching values of numbers (the number 5 in a single copy is not a matching value)

 
Artyom Trishkin:
Array:

1,1,1,2,3,3,2,1,4,4,5

Sort by:
1,1,1, 1,2, 2, 3,3,4,4,5

The output is 4 matching values of numbers (the number 5 in a single copy is not a matching value)

This would then read roughly: determine the number of numbers in the sequence that have duplicates.
 
Alexey Kozitsyn:
Then it would sound something like: determine the number of numbers in a sequence that have duplicates.
Maybe. It's not about the wording at the moment, it's about the solution. I'm sitting here, solving...
 
Artyom Trishkin:

Neither.

There are four known unknown numbers. You need to find the number of repeated numbers as in the example in my first post.

If the order is unimportant, the numbers are integers and the range is known, then you can count for O(size) simply by creating an array of counters.

ArrayResize(counter,100);

ArrayInitialize(counter,0);

for(int i=ArraySize(source)-1;i>=0;i--) {

   counter[source[i]]‌++;

}

int pos=ArrayMaximum(counter);‌

‌PrintFormat("Чаще всего встречалось число %d, аж %d раз",pos,source[pos]);

Otherwise, really sort and from there select the longest sequence of identical numbers.
 
Maxim Kuznetsov:

If the order is unimportant, the numbers are integers and the range is known, then you can calculate for O(size) simply by creating an array of counters.

ArrayResize(counter,100);

ArrayInitialize(counter,0);

for(int i=ArraySize(source)-1;i>=0;i--) {

   counter[source[i]]‌++;

}

int pos=ArrayMaximum(counter);‌

‌PrintFormat("Чаще всего встречалось число %d, аж %d раз",pos,source[pos]);

Otherwise, really - sort and from there select the longest sequence of identical ones.
So "otherwise" is an array of numbers of any type. So it's a template function. Well and sort and search.
However, I'm doing it slowly.
 
Artyom Trishkin:
Maybe. It's not about the wording at the moment, it's about the solution. I'm sitting here solving...
It's weird.
 
fxsaber:
That's odd.
Your option always gives out 1. It takes longer to figure out than it does to come up with your own. That's all the weirdness ;)
 
Artyom Trishkin:
Your option always gives out 1. It takes longer to figure out than it does to come up with your own. That's all the weirdness ;)
Running it as it is.
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 1;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > Res)
          Res = Tmp;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2};
  
  Print(Strange(Array));
}
It works.
 

This one seems to be too:

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

int GetM(int &Mas[])
{
int c=0,cd=0,res=-1;

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>=cd) { // ищем первое большее ">" или максимально большее ">=" при одинаковом количестве
     cd=c; // количество совпадений
     res=Mas[i]; // число
   }
   c=0;
  }
  return( res /*cd*/); // число|количество
}