code_junkie

How can I get the unique values of an array in .net?

Nov 10th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. string[] OutputArray = MyArray.Distinct().ToArray();
  2.  
  3. int[] test = { 1, 2, 1, 3, 3, 4, 5 };
  4. var res = (from t in test select t).Distinct<int>();
  5. foreach (var i in res)
  6. {
  7. Console.WriteLine(i);
  8. }
  9.  
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15.  
  16. string[] myarray = new string[] { "aaa", "bbb", "aaa" };
  17. Dictionary mydict = new Dictionary();
  18. foreach (string s in myarray) {
  19. if (!mydict.ContainsKey(s)) mydict.Add(s, s);
  20. }
Add Comment
Please, Sign In to add comment