views:

159

answers:

2

hi all, how can i avoid duplicates from a string (in c#) eg.i have a,a,b,b,c i want to get the answer like a,b,c

+3  A: 

By using HashSet<string>.

Darin Dimitrov
+1  A: 

You could use a List<> and the Contains method to check for this.

Declare it as

List<string> list = new List<string>();

and check as

if (!list.Contains(stringValue))
   list.Add(stringValue);
astander
Note: This algorithm is O(n^2) for populating a list with n elements. Only use for small lists.
Mark Byers