Using LINQ:
var uniquevalues = list.Distinct();
It gives you IEnumerable<string>.
If you need an array:
string[] uniquevalues = list.Distinct().ToArray();
If you are not using .NET 3.5, this is a little trickier:
List<string> newList = new List<string>();
foreach (string s in list)
{
if (!newList.Contains(s))
newList.Add(s);
}
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);
source
share