C # Deletes a repeating element instance from an array

Source: Internet
Author: User

Example 1

The code is as follows Copy Code

String[] Stringarray = {"AAA", "BBB", "AAA", "CCC", "BBB", "DDD", "CCC", "AAA", "BBB", "ddd"};
List is used to store the different elements taken out of the array
list<string> liststring = new list<string> ();
foreach (String eachstring in Stringarray)
{
if (!liststring.contains (eachstring))
Liststring.add (eachstring);
}
Finally, take each string out of the list and manipulate it.
foreach (String eachstring in liststring)
{
Console.Write (eachstring); Print each string
}

Example 2

  code is as follows copy code

//1. Converts a string to an array of

String ls_str= "122,158,141,236,896,123,122,158,141";

String[] Str_num=ls_str. Split (', ');

 //2. Define ArrayList to determine that the repeating field in the array is not added to the ArrayList object

   ArrayList al = new ArrayList ();
   for (int i = 0; i < Str_num. Length; i++)
    {

//To determine if an already exists
     if (al. Contains (str_num[i]) = = False)
        {
            al. ADD (Str_num[i]);
        }

   }

 //3. Convert ArrayList to array
   str_num= new String[al. Count];
   str_num= (string[]) al. ToArray (typeof (String));

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.