Using LINQ:
var uniquevalues = list.Distinct();
That gives you an IEnumerable<string>
.
If you want an array:
string[] uniquevalues = list.Distinct().ToArray();
If you are not using .NET 3.5, it's a little more complicated:
List<string> newList = new List<string>();
foreach (string s in list)
{
if (!newList.Contains(s))
newList.Add(s);
}
// newList contains the unique values
Another solution (maybe a little faster):
Dictionary<string,bool> dic = new Dictionary<string,bool>();
foreach (string s in list)
{
dic[s] = true;
}
List<string> newList = new List<string>(dic.Keys);
// newList contains the unique values