-
You couldn't search the web for “remove duplicates from sorted array?”
How To Ask Questions The Smart Way. 2004
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up. -
Is that so hard you couldn't even attempt it? Since it is already sorted it's easy, copy the array to itself skipping the duplicates:
Not compiled, not tested, just typed.
template<typename T> int remove_duplicates(T& arr[]){ // From a sorted array. int iEnd = ArraySize(arr); if(iEnd <= 1) return iEnd; int iLast = iEnd - 1; int iOut = 0; for(int iBeg=0; iBeg < iLast; ++iBeg) if(arr[iBeg] != arr[iBeg+1]) arr[iOut++] = arr[iBeg]; arr[iOut++] = arr[iLast]; return iOut; }
Not compiled, not tested, just typed.
-
Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
No free help 2017.04.21Or pay someone. Top of every page is the link Freelance.
Hiring to write script - General - MQL5 programming forum 2018.05.12
Please Use MathUnique() from the standard library.
https://www.mql5.com/en/docs/standardlibrary/mathematics/stat/mathsubfunctions/statmathunique
#include <Math\Stat\Math.mqh> void OnStart() { int myarray[] = {1,2,2,4,7,7,7,16,20,20}; int no_dups[]; MathUnique(myarray, no_dups); ArrayPrint(no_dups); } // 1 2 4 7 16 20
![Documentation on MQL5: Standard Library / Mathematics / Statistics / Subfunctions / MathUnique Documentation on MQL5: Standard Library / Mathematics / Statistics / Subfunctions / MathUnique](https://c.mql5.com/36/40/documentation-on-mql5-standard__3.png)
- www.mql5.com
Step 1) Sort the array
Step 2) define a 'for' loop
Step 3) define an 'if' in the loop and avoid repetitive elements
Please Use MathUnique() from the standard library.
https://www.mql5.com/en/docs/standardlibrary/mathematics/stat/mathsubfunctions/statmathunique
The MathUnique is a really good way - thanks for mentioning it.
If the original poster needs a loop method, here is one
int arraySrc[] = {1,2,2,4,161, 7,7,7, 77, 16,200, 20,20}; int arrayDst[] = {}; int elements = 0; ArraySort(arraySrc); ArrayResize(arrayDst, ArraySize(arraySrc)); ZeroMemory(arrayDst); int lastInsert = 0; for(int i=0; i<ArraySize(arraySrc); i++) { if((i==0) || (arraySrc[i] != lastInsert)) { lastInsert = arrayDst[i] = arraySrc[i]; elements++; } } ArraySort(arrayDst); ArrayRemove(arrayDst, 0, ArraySize(arrayDst)-elements); Print("//--ArrayLoop Method - " + " elements = " + IntegerToString(elements)); ArrayPrint(arraySrc); ArrayPrint(arrayDst);
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I am new to coding.
I have an int. array that looks like (1,2,2,4,7,7,7,16,20,20)
How can I remove the duplicates to make look like (1,2,4,7,16,20)
I will be truly thankful for your help.