Array deduplication (implemented in C)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.