c – Zero out repeated values ​​of an array

Question:

Hey guys. I have an exercise where I must populate an array with random numbers from 0 to 9 and then I must replace the repeated numbers (except the first time the number appears) with the number 0.
The first part I managed to do, but I'm having difficulty resetting the repeated values. Here's the code:

``````    #include <stdio.h>
#include <stdlib.h>
#include <time.h>

main ()
{
int N, random, coluna, linha, temp, l, k;
printf("Insira o tamanho da matriz\n");
scanf("%d", &N);
int matriz[N][N];
srand(time(NULL));

// Populei a matriz com números aleatórios
for (coluna = 0 ; coluna < N ; coluna++){
for(linha = 0 ; linha < N ; linha++){
random = rand () % 10;
matriz[coluna][linha] = random;
printf("%d ", matriz[coluna][linha]);
}
printf("\n");
}
printf("\n\n\n");

// A maneira que pensei para comparar a matriz não deu muito certo...
for (coluna = 0 ; coluna < N ; coluna++){
for (linha = 0 ; linha < N ; linha++){
for (k = coluna ; k < N ; k++){
for (l = 1 ; l < N ; l++){
if (matriz[coluna][linha] == matriz[k][l]){
matriz[coluna][linha] = 0;
}
}
}
}
}

for (coluna = 0 ; coluna < N ; coluna++){
for(linha = 0 ; linha < N ; linha++){
printf("%d ", matriz[coluna][linha]);
}
printf("\n");
}
``````

}

The secret is to create an integer vector with 10 positions `int cont[ 10 ]` count `int cont[ 10 ]` , each position will be an independent counter for each number (from 0 to 9).

Here is the tested and commented solution:

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_COLS     (16)
#define MAX_LINS     (16)

int main( int argc, char * argv[] )
{
int i = 0;
int cl = 0;
int ln = 0;
int matriz[ MAX_COLS ][ MAX_LINS ];
int cont[ 10 ];

/* Utiliza a data/hora atual como semente */
srand(time(NULL));

/* Inicializa a matriz de contagem */
for( i = 0; i < 10; i++ )
cont[i] = 0;

/* Populando matriz com numeros aleatorios */
for( cl = 0; cl < MAX_COLS; cl++ )
for( ln = 0; ln < MAX_LINS; ln++ )
matriz[cl][ln] = rand() % 10;

/* Exibe matriz gerada aleatoriamente */
for( cl = 0; cl < MAX_COLS; cl++ )
{
for( ln = 0; ln < MAX_LINS; ln++ )
printf( "%d ", matriz[cl][ln] );

printf("\n");
}

printf("\n");

/* Conta as repeticoes de cada numero na matriz e substitui por zero */
for( cl = 0; cl < MAX_COLS; cl++ )
for( ln = 0; ln < MAX_LINS; ln++ )
if( cont[ matriz[cl][ln] ]++ )
matriz[cl][ln] = 0;

for( cl = 0; cl < MAX_COLS; cl++ )
{
for( ln = 0; ln < MAX_LINS; ln++ )
printf( "%d ", matriz[cl][ln] );

printf("\n");
}

printf("\n");

return 0;
}

/* fim-de-arquivo */
``````

Exit:

``````8 4 2 3 0 6 2 0 7 0 7 9 4 7 9 2
6 7 2 6 3 4 5 2 2 5 7 2 1 8 9 8
3 5 6 7 3 4 0 6 7 7 4 1 7 6 5 4
6 7 2 3 1 8 9 1 7 6 5 9 4 9 0 9
9 9 0 5 1 3 4 2 2 5 4 7 8 2 6 1
4 1 5 5 2 7 0 2 4 4 6 8 4 3 2 3
8 1 6 1 8 0 1 1 3 7 1 5 5 7 6 9
4 2 4 5 6 5 2 7 8 8 7 8 3 4 2 4
7 8 1 8 4 4 8 1 9 4 2 7 7 2 2 0
0 3 8 0 1 6 8 2 8 4 0 9 5 5 3 0
9 0 1 6 9 3 0 2 1 7 2 9 4 8 5 6
3 0 2 9 4 9 4 4 2 9 0 1 5 3 9 8
2 3 3 6 5 8 3 3 4 2 7 3 6 2 1 3
9 1 9 6 9 3 0 2 4 6 8 4 9 2 7 5
9 1 4 4 1 8 1 5 3 7 8 8 8 8 6 7
8 9 7 8 4 3 2 1 4 7 2 7 5 0 4 3

8 4 2 3 0 6 0 0 7 0 0 9 0 0 0 0
0 0 0 0 0 0 5 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0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