If you always have the ids in sorted order like in your example, then the solution can be done more efficiently(one pass over 2 or O(n) vs O(n^2)) compared to ARUKARI's solution.
PHP Code:
getUniqueElements(input, numElements, output, &numUnique)
{
new currentValue = -1;
for(new i = 0; i < numElements; i++)
{
if(arr[i] != currentValue)
{
if(currentValue != -1)
{
output[numUnique++] = currentValue;
}
currentValue = arr[i];
}
}
output[numUnique++] = currentValue;
}
My code doesn't change the original array, it extracts the data into a new one, but the solution can be modified to work in place. I leave this to you, as practice.
Note that I assumed your ids are >0. The code will not work correctly if some ids are -1. You also need to handle the case when the array is empty(just check before doing anything).
If the ids are not sorted, then you can still do it in only one pass(O(n)), by using a trie(assuming the trie implementation inside AMXX provides O(1) access on average). If it is a tree-based implementation, then likely it provides O(log(n)) access which results in an O(n * log(n)) overall complexity, which is still better than O(n^2).
Keep in mind that this matters only when the size of your array grows. If your array is small, then any solution will do just fine.
Below is the solution using a trie:
PHP Code:
getUniqueElements(input, numElements, output, &numUnique)
{
new Trie:set = TrieCreate();
new strKey[12];
for(new i = 0; i < numElements; i++)
{
num_to_str(input[i], strKey, charsmax(strKey));
if(!TrieKeyExists(set, strKey))
{
output[numUnique++] = arr[i];
TriePushCell(set, strKey, 0);
}
}
TrieDestroy(set);
}
Here is how you can call either of these 2 functions:
PHP Code:
//assuming you have an array ids you want to process with numIds ids inside it
new uniqueIds[SIZE_HERE]
int numUnique;
getUniqueElements(ids, numIds, uniqueIds, numUnique)
for(int i = 0; i < numUnique; i++)
{
//do something with uniqueIds[i]
}