[My solution C language interview series] 008 remove repeated numbers in the array
Remove repeated numbers in the array
There is an array with a size of 100. The numbers in the array are between 1 and 99, but the numbers in the array are repeated. Please write a function to remove the repeated numbers in the array.
# Define init_num-1
Method 1: (the easiest way to think)
Void removebufferrepnum_00 (INT buffer [])
{
Int I, J;
For (I = 0; I <buffersize; I ++)
{
For (j = I + 1; j <buffersize; j ++)
{
If (buffer [I] = buffer [J])
{
Buffer [I] = init_num;
Break;
}
}
}
For (I = 0, j = 0; I <buffersize; I ++)
{
If (buffer [I] = init_num)
Continue;
Buffer [J ++] = buffer [I];
}
While (j <buffersize)
Buffer [J ++] = init_num;
}
This algorithm is the simplest, and the time complexity is O (n2)
Method 2: (use the hash table method)
Void removebufferrepnum_01 (INT buffer [])
{
Int tbuffer [buffersize];
Int I = 0, j = 0;
For (I = 0; I <buffersize; I ++) // initializes the Array
Tbuffer [I] = init_num;
For (I = 0; I <buffersize; I ++) // remove an algorithm
{
If (tbuffer [buffer [I] = init_num)
Tbuffer [buffer [I] = buffer [I];
}
For (I = 0; I <buffersize; I ++)
{
If (tbuffer [I] = init_num)
Continue;
Buffer [J ++] = tbuffer [I];
}
While (j <buffersize)
Buffer [J ++] = init_num;
}
This method is implemented by enabling the auxiliary space and setting the hash table. It can be executed n times in total. Time Complexity: O (n ). However, the only weakness is that extra space is required.