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