ArrayBsearch doesn't search as it should be

 
HI, I have opened some positions with one magic number, 
and then on the same currency pair, have opened some more positions with different magic number
Now trying to store only magic numbers in an array, results are not right
Could someone kindly point out where is the mistake
void UpdateMagicNumbers()  // count magic number
   {
   long mNumber[]; 
   ArrayResize(mNumber,1); ArrayInitialize(mNumber,0);
   int cycle = PositionsTotal()-1;
   for(cycle; cycle>=0; cycle--)
      {
      if(PositionGetSymbol(cycle) == _Symbol)
         {
         long tempMagic = PositionGetInteger(POSITION_MAGIC);
         int arrSize = ArraySize(mNumber);
         Print(PositionGetSymbol(cycle)," tempMagic ",tempMagic," - cycle(",cycle,") - through all Positions - ArraySize mNumber[",arrSize,"]");
         for (int x=0; x<arrSize && tempMagic>0; x++)
            {
            arrSize = ArraySize(mNumber);
            ArraySort(mNumber);
            if(mNumber[ArrayBsearch(mNumber,tempMagic)] != tempMagic)
               {
               ArrayResize(mNumber,arrSize+1);
               mNumber[arrSize] = tempMagic; 
               }
            }
         }
      }
   ArrayResize(mNumber,ArraySize(mNumber)-1);
   //ArraySort(mNumber);
   ArrayPrint(mNumber);
   }

Open positions:




Result:





Result
 
  1. The result is exactly as expected. You start with an array with an initial zero element, insert uniquely into the array, and then delete the last value (a ten).

  2.          for (int x=0; x<arrSize && tempMagic>0; x++)
    

    I have no idea why you try to uniquely insert n² times. Once is sufficient.

  3. I suggest you instead start with an empty array, insert all elements, sort the array once, and then remove the duplicates.

    Not tested, not compiled, just typed.

    #define INDEX uint
    template<typename Ti, typename To>
    INDEX    unique_copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
                                        To& out[], INDEX oBeg){
       if(iBeg == iEnd)  return oBeg;
       Ti    prev  = out[oBeg++]  = inp[iBeg];
       while(++iBeg != iEnd){
          if(!(prev == inp[iBeg]) )  prev  = out[oBeg++]  = inp[iBeg];
       }
       return oBeg;
    }
    ///////////////////////////////////////////////
       long mNumber[]; int arrSize=0;
       
       for(int cycle = PositionsTotal()-1; cycle>=0; --cycle)
          if(PositionGetSymbol(cycle) == _Symbol)
             {
             long tempMagic = PositionGetInteger(POSITION_MAGIC);
             if(tempMagic > 0){
               ArrayResize(mNumber, arrSize+1);
              mNumber[arrSize++] = PositionGetInteger(POSITION_MAGIC);
             }
        }
    
       ArraySort(mNumber)
    
       arrSize = unique_copy(mNumber, 0, size, mNumber, 0);
       ArrayResize(mNumber,arrSize);
    
       ArrayPrint(mNumber);
    Not tested, not compiled, just typed.
 
William Roeder #:
  1. The result is exactly as expected. You start with an array with an initial zero element, insert uniquely into the array, and then delete the last value (a ten).

  2. I have no idea why you try to uniquely insert n² times. Once is sufficient.

  3. I suggest you instead start with an empty array, insert all elements, sort the array once, and then remove the duplicates.

    Not tested, not compiled, just typed.

    Not tested, not compiled, just typed.

I got your point, thanks mate 

Reason: