Test if all values in a list are unique

后端 未结 8 1956
温柔的废话
温柔的废话 2020-12-03 09:22

I have a small list of bytes and I want to test that they\'re all different values. For instance, I have this:

List theList = new List

        
相关标签:
8条回答
  • 2020-12-03 09:54

    One can also do: Use Hashset

    var uniqueIds = new HashSet<long>(originalList.Select(item => item.Id));
    
                if (uniqueIds.Count != originalList.Count)
                {
                }
    
    0 讨论(0)
  • 2020-12-03 09:55
    bool isUnique = theList.Distinct().Count() == theList.Count();
    
    0 讨论(0)
  • 2020-12-03 09:59

    And another solution, if you want to find duplicated values.

    var values = new [] { 9, 7, 2, 6, 7, 3, 8, 2 };
    
    var sorted = values.ToList();
    sorted.Sort();
    for (var index = 1; index < sorted.Count; index++)
    {
        var previous = sorted[index - 1];
        var current = sorted[index];
        if (current == previous)
            Console.WriteLine(string.Format("duplicated value: {0}", current));
    }
    

    Output:

    duplicated value: 2
    duplicated value: 7
    

    http://rextester.com/SIDG48202

    0 讨论(0)
  • 2020-12-03 10:03

    Okay, here is the most efficient method I can think of using standard .Net

    using System;
    using System.Collections.Generic;
    
    public static class Extension
    {
        public static bool HasDuplicate<T>(
            this IEnumerable<T> source,
            out T firstDuplicate)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
    
            var checkBuffer = new HashSet<T>();
            foreach (var t in source)
            {
                if (checkBuffer.Add(t))
                {
                    continue;
                }
    
                firstDuplicate = t;
                return true;
            }
    
            firstDuplicate = default(T);
            return false;
        }
    }
    

    essentially, what is the point of enumerating the whole sequence twice if all you want to do is find the first duplicate.

    I could optimise this more by special casing an empty and single element sequences but that would depreciate from readability/maintainability with minimal gain.

    0 讨论(0)
  • 2020-12-03 10:05

    I check if an IEnumerable (aray, list, etc ) is unique like this :

    var isUnique = someObjectsEnum.GroupBy(o => o.SomeProperty).Max(g => g.Count()) == 1;
    
    0 讨论(0)
  • 2020-12-03 10:11

    Here's another approach which is more efficient than Enumerable.Distinct + Enumerable.Count (all the more if the sequence is not a collection type). It uses a HashSet<T> which eliminates duplicates, is very efficient in lookups and has a count-property:

    var distinctBytes = new HashSet<byte>(theList);
    bool allDifferent = distinctBytes.Count == theList.Count;
    

    or another - more subtle and efficient - approach:

    var diffChecker = new HashSet<byte>();
    bool allDifferent = theList.All(diffChecker.Add);
    

    HashSet<T>.Add returns false if the element could not be added since it was already in the HashSet. Enumerable.All stops on the first "false".

    0 讨论(0)
提交回复
热议问题