C # operations on multiple sets and arrays (merge, deduplicate, and Judge)

Source: Internet
Author: User
In the development process, array and set processing are the most worrying. for or foreach is generally used to process some operations. Here we will introduce some common set and array operation functions.

First, we will give an example of two sets, A and B.

List <int> listA = new List <int> {1, 2, 3, 5, 7, 9 };

List <int> listB = new List <int> {13, 4, 17,29, 2 };

 

ListA. addRange (listB); sets. B merge List <int> Result = listA. union (listB ). toList <int> (); // remove duplicate items List <int> Result = listA. concat (listB ). toList <int> (); // retained duplicate items

ListA. BinarySearch ("1"); // determines whether a value is included in the set. If yes, 0 is returned.

 

In the example of two Arrays

Int [] I = new int [] {1, 2}; int [] j = new int [] {2, 3}; List <int> r = new List <int> ();

R. AddRange (I );

R. AddRange (j );

Int [] c = r. ToArray (); merge Arrays

Int [] x = I. Union (j). ToArray <int> (); // remove duplicate items

Int [] x = I. Concat (j). ToArray <int> (); // retained duplicate items

Int n = Array. BinarySearch (I, 3); // determines whether the Array contains a value. If it contains, 0 is returned.

 

 

 

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.