How to remove repeated elements from Arrays

Source: Internet
Author: User

// Original] how to remove repeated elements from Arrays

//


# Include <stdio. h>

Int main ()

{

Int a [5];

Printf ("input five numbers :");

For (int I = 0; I <5; I ++)

{

Scanf ("% d", & a [I]);

}

Int len = 5; // 5 5 5 2 5

// Two for loops are used to traverse

For (int j = 0; j <len; ++ j)

{

For (int m = j + 1; m <len; ++ m)

{

If (a [j] = a [m])

{

// Use the overwrite method to delete the same element as the first for statement.

For (int k = m + 1; k <len; ++ k) // there will be differences in writing-According to k = m + 1, may be a len-1 or other...

A [k-1] = a [k];

Len --; // keep up with the length

-- M; // because an element is deleted, the subscript of the array element to be compared with a [j] does not change but the value has changed)

}

}

}

For (int n = 0; n <len; ++ n)

{

Printf ("% d", a [n]);

}

Printf ("\ n ");

Return 0;

}


This article is from the "Jean_Z learning blog" blog, please be sure to keep this source http://7905490.blog.51cto.com/7895490/1296747

Related Article

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.