#include <stdio.h>
void printUnique(int* in, int n, int* out)
{
printf("* -----------------\n"); printf("* numero de items de entrada: %d\n", n
);
for (int i = 0; i < n ; i++)
printf("* in[%d] = %d\n", i
, in
[i
]);
for (int i = 0; i < n; i++)
{
out[i] = in[i];
for (int j = i + 1; j < n; j++)
{
if (in[i] == in[j])
{
// numeros em i e i+1 sao iguais, entao a partir de i+1,
// deslocar todos os numeros 1 posicao 'a esquerda,
// matando o numero repetido em i+1, e perdendo o ultimo
// numero 'a direita...por isso, la' em baixo n vai precisar
// ser decrementado, e j tambem vai precisar ser decrementado,
// para comparar o numero em i com o novo numero em i+1
for (int k = j+1; k < n; k++)
in[k-1] = in[k];
n--; // total de numeros diminuiu
j--; // precisa testar de novo a mesma posicao
} // if
} // for j
} // for i
printf("* numero de items unicos: %d\n", n
); for (int i = 0; i < n; i++)
{
printf("* out[%d] = %d\n", i
, out
[i
]); }
}
int main(void)
{
int n;
int in[10];
int out[10];
printUnique(in, 0, out);
in[0] = 1;
printUnique(in, 1, out);
in[0] = 1; in[1] = 1;
printUnique(in, 2, out);
in[0] = 1; in[1] = 2;
printUnique(in, 2, out);
in[0] = 1; in[1] = 1; in[2] = 1;
printUnique(in, 3, out);
in[0] = 1; in[1] = 2; in[2] = 2;
printUnique(in, 3, out);
in[0] = 1; in[1] = 2; in[2] = 3;
printUnique(in, 3, out);
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIHByaW50VW5pcXVlKGludCogaW4sIGludCBuLCBpbnQqIG91dCkKewogICBwcmludGYoIiogLS0tLS0tLS0tLS0tLS0tLS1cbiIpOwogICBwcmludGYoIiogbnVtZXJvIGRlIGl0ZW1zIGRlIGVudHJhZGE6ICVkXG4iLCBuKTsKCiAgIGZvciAoaW50IGkgPSAwOyBpIDwgbiA7IGkrKykKICAgICAgcHJpbnRmKCIqIGluWyVkXSA9ICVkXG4iLCBpLCBpbltpXSk7CgogICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgewogICAgICAgb3V0W2ldID0gaW5baV07CgogICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgbjsgaisrKQogICAgICAgewogICAgICAgICAgIGlmIChpbltpXSA9PSBpbltqXSkKICAgICAgICAgICB7CiAgICAgICAgICAgICAgLy8gbnVtZXJvcyBlbSBpIGUgaSsxIHNhbyBpZ3VhaXMsIGVudGFvIGEgcGFydGlyIGRlIGkrMSwKICAgICAgICAgICAgICAvLyBkZXNsb2NhciB0b2RvcyBvcyBudW1lcm9zIDEgcG9zaWNhbyAnYSBlc3F1ZXJkYSwKICAgICAgICAgICAgICAvLyBtYXRhbmRvIG8gbnVtZXJvIHJlcGV0aWRvIGVtIGkrMSwgZSBwZXJkZW5kbyBvIHVsdGltbwogICAgICAgICAgICAgIC8vIG51bWVybyAnYSBkaXJlaXRhLi4ucG9yIGlzc28sIGxhJyBlbSBiYWl4byBuIHZhaSBwcmVjaXNhcgogICAgICAgICAgICAgIC8vIHNlciBkZWNyZW1lbnRhZG8sIGUgaiB0YW1iZW0gdmFpIHByZWNpc2FyIHNlciBkZWNyZW1lbnRhZG8sCiAgICAgICAgICAgICAgLy8gcGFyYSBjb21wYXJhciBvIG51bWVybyBlbSBpIGNvbSBvIG5vdm8gbnVtZXJvIGVtIGkrMQogICAgICAgICAgICAgIGZvciAoaW50IGsgPSBqKzE7IGsgPCBuOyBrKyspCiAgICAgICAgICAgICAgICAgIGluW2stMV0gPSBpbltrXTsKICAgICAgICAgICAgICBuLS07IC8vIHRvdGFsIGRlIG51bWVyb3MgZGltaW51aXUKICAgICAgICAgICAgICBqLS07IC8vIHByZWNpc2EgdGVzdGFyIGRlIG5vdm8gYSBtZXNtYSBwb3NpY2FvCiAgICAgICAgICAgfSAvLyBpZgogICAgICAgfSAvLyBmb3IgagogICB9IC8vIGZvciBpCgogICBwcmludGYoIiogbnVtZXJvIGRlIGl0ZW1zIHVuaWNvczogJWRcbiIsIG4pOwogICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgewogICAgICAgcHJpbnRmKCIqIG91dFslZF0gPSAlZFxuIiwgaSwgb3V0W2ldKTsKICAgfQp9CgppbnQgbWFpbih2b2lkKQp7CiAgIGludCBuOwogICBpbnQgaW5bMTBdOwogICBpbnQgb3V0WzEwXTsKCiAgIHByaW50VW5pcXVlKGluLCAwLCBvdXQpOwoKICAgaW5bMF0gPSAxOwogICBwcmludFVuaXF1ZShpbiwgMSwgb3V0KTsKCiAgIGluWzBdID0gMTsgaW5bMV0gPSAxOwogICBwcmludFVuaXF1ZShpbiwgMiwgb3V0KTsKCiAgIGluWzBdID0gMTsgaW5bMV0gPSAyOwogICBwcmludFVuaXF1ZShpbiwgMiwgb3V0KTsKCiAgIGluWzBdID0gMTsgaW5bMV0gPSAxOyBpblsyXSA9IDE7CiAgIHByaW50VW5pcXVlKGluLCAzLCBvdXQpOwoKICAgaW5bMF0gPSAxOyBpblsxXSA9IDI7IGluWzJdID0gMjsKICAgcHJpbnRVbmlxdWUoaW4sIDMsIG91dCk7CgogICBpblswXSA9IDE7IGluWzFdID0gMjsgaW5bMl0gPSAzOwogICBwcmludFVuaXF1ZShpbiwgMywgb3V0KTsKfSAgICAgICAgICAgIA==