#include <iostream>
template <typename T>
void unique(T* t, std::size_t& size)
{
T* end = t + size;
// find the first duplicate:
T* current = t;
while (current != end && current + 1 != end && *current != *(current + 1))
++current;
++current;
// point source at the next different element
T* source = current + 1;
while (source != end && source + 1 != end && *source == *current)
++source;
while (source != end)
{
*current++ = *source++;
// advance source to the next different element
if (source != end && *(source - 1) == *source)
++source;
}
// current is pointing to the new end element, so:
size = current - t;
}
template <typename T>
void print(const T& ra_container, std::size_t size)
{
std::cout << "[ ";
for (std::size_t i = 0; i < size; ++i)
std::cout << ra_container[i] << ' ';
std::cout << ']';
}
int main()
{
int data[2][10] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 9}, {2, 2, 4, 4, 6, 6, 8, 8, 10, 10} };
std::size_t sizes[2] = { 10, 10 };
for (std::size_t i = 0; i < 2; ++i)
{
print(data[i], sizes[i]);
std::cout << " => ";
unique(data[i], sizes[i]);
print(data[i], sizes[i]);
std::cout << '\n';
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CnZvaWQgdW5pcXVlKFQqIHQsIHN0ZDo6c2l6ZV90JiBzaXplKQp7CiAgICBUKiBlbmQgPSB0ICsgc2l6ZTsKCiAgICAvLyBmaW5kIHRoZSBmaXJzdCBkdXBsaWNhdGU6CiAgICBUKiBjdXJyZW50ID0gdDsKICAgIHdoaWxlIChjdXJyZW50ICE9IGVuZCAmJiBjdXJyZW50ICsgMSAhPSBlbmQgJiYgKmN1cnJlbnQgIT0gKihjdXJyZW50ICsgMSkpCiAgICAgICAgKytjdXJyZW50OwoKICAgICsrY3VycmVudDsKCiAgICAvLyBwb2ludCBzb3VyY2UgYXQgdGhlIG5leHQgZGlmZmVyZW50IGVsZW1lbnQKICAgIFQqIHNvdXJjZSA9IGN1cnJlbnQgKyAxOwogICAgd2hpbGUgKHNvdXJjZSAhPSBlbmQgJiYgc291cmNlICsgMSAhPSBlbmQgJiYgKnNvdXJjZSA9PSAqY3Vycm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