I think we should first perform array deduplication when practicing ACM in nine degrees, because most of my work is written in PHP. Code In PHP, only one array_diff () function can be used to deduplicate arrays. However, in C language, I don't have any ready-made API functions that can be called by me, So I implemented Algorithm Array deduplication, but this algorithm has obvious defects. I will first display the algorithm and then explain the array of defects to deduplicate the code.
# Include <stdio. h> # include <stdlib. h> # define false-1int arraydiff (int * a, int Max, int Len); int main () {int A [100]; int N, K, I, Len, max; while (scanf ("% d", & N )! = EOF) {// receive client data for (I = 0; I <n; I ++) {scanf ("% d", & A [I]);} scanf ("% d", & K); // find the maximum number of a [I] Max = A [0]; for (I = 1; I <N; I ++) {If (max <A [I]) max = A [I];} // array deduplication from small to large sorting Len = arraydiff (, max, n); // outputs the k-th largest number printf ("% d \ n", a [k-1]);} return 0 ;} /*** Description: array deduplication algorithm */INT arraydiff (int * a, int Max, int Len) {int arrayflag [Max + 1]; int I, J; // initialize the flag array for (I = 0; I <= max; I ++) {arrayflag [I] = false;} // remove the algorithm for (I = 0; I <Len; I ++) {arrayflag [A [I] = A [I];} // retrieve the valid number for (I = 0, j = 0; I <= max; I ++) {If (arrayflag [I]! = False) {A [J ++] = arrayflag [I] ;}} return J ;}
Because the algorithm needs to re-apply for array space, and the size of the array space is based on the maximum number of de-duplicated arrays, this will be very extreme, my array int A [4] = {112000,11200, 112000}. Although the size of array a to be de-duplicated is 4, I have to re-apply for a new memory space, this is a huge waste of resources. I hope you can reply to me and write out the array deduplication algorithm that you think is better. We recommend that you use C to implement the Code. Anyone who gives me a message will reply to me.