C Operation Methods for Multiple Sets and Arrays of Merge Deduplicate Judge

  • 2021-11-13 18:00:30
  • OfStack

In the development process. Array and collection of processing is the most worrying. 1 will use for or foreach to deal with some operations. Here introduced some commonly used collection and array operation functions.

First, 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); Merge the set A. B

List < int > Result = listA.Union(listB).ToList < int > (); //Eliminate duplicates

List < int > Result = listA.Concat(listB).ToList < int > (); //Keep duplicates

listA. BinarySearch ("1"); //Determines whether the collection contains a value. Returns 0 if it does

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 array

int[] x=i.Union(j).ToArray < int > (); //Eliminate duplicates

int[] x=i.Concat(j).ToArray < int > (); //Keep duplicates

int n = Array. BinarySearch (i, 3); //Determines whether an array contains a value. Returns 0 if it does


Related articles: