Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static int[] unique(int[] ids)
- {
- IDictionary<int, object> d = new Dictionary<int, object>();
- foreach(int i in ids)
- d[i] = null;
- int[] results = new int[d.Count];
- int j = 0;
- foreach(int id in d.Keys)
- results[j++] = id;
- return results;
- }
- int[] x = new[] { 1, 4, 23, 4, 1 };
- int[] distinct = x.Distinct().ToArray();
- // distinct is now { 1, 4, 23 } (but not necessarily in that order)
- var vals = new List<int> { 1, 2, 3, 2, 1, 2, 3, 2, 3, 4, 3, 2, 3 };
- vals.Sort();
- var uniques = new HashSet<int>(vals);
- List<int> vals = new List<int>();
- vals.Add(1);
- vals.Add(2);
- vals.Add(3);
- vals.Add(2);
- ...
- vals.Sort();
- List<int> uniques = new List<int>();
- vals.ForEach(delegate(int v) {
- if (!uniques.Contains(v)) uniques.Add(v);
- });
- public IEnumerable<int> unique(int[] ids)
- {
- List<int> l = new List<int>();
- foreach (int id in ids)
- {
- if (!l.Contains(id))
- {
- l.Add(id);
- yield return id;
- }
- }
- }
- private static List<T> GetUnique<T>(List<T> list) where T : IEquatable<T>
- {
- list.Sort();
- int count = list.Count;
- List<T> unique = new List<T>(count);
- T last = default(T);
- for (int i = 0; i < count; i++)
- {
- T val = list[i];
- if (i != 0 && last.Equals(val)) continue;
- last = val;
- unique.Add(val);
- }
- return unique;
- }
Add Comment
Please, Sign In to add comment