One-Bit Array removes duplicates

Source: Internet
Author: User

Some people will encounter some small problems during the interview. Although these problems are small, they can display your past work conditions and the status of programming.
The following example shows how to remove duplicates from arrays. If you encounter an interview again, you can proceed.
 
Example: Write a method to remove repeated elements in the array string [] a = new string [] {"a", "B", "c", ".
A: The method described here uses the generic type. Therefore, you must first reference a namespace: System. Collections.
 
Using System. Collections;


Public static ArrayList RemoveRepeat (string [] strRepeat)
{
ArrayList list = new ArrayList (); // defines a generic element used to hold arrays.
Foreach (string str in strRepeat) // The foreach loops through the elements of the array.
{
If (list. Contains (str) = false &&! String. IsNullOrEmpty (str) // list. Contains (str) checks whether the same element exists in the list. list. Contains (str) = false when not both are true
List. Add (str); // Add different elements in the array to the list.
}
Return list; // return list. The value in the list is the expected result.
}
 
 
In this example, the key is to use the "generic" method Contains () to determine whether the list Contains the same elements.
For this "array de-duplication", many people will use the method of comparing a single element with the entire array element one by one, and compare it with the above example to determine the efficiency.
 

 

From Feng Hu

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.