Check if all items are the same in a List

C#vb.netListLinq to-Objects

C# Problem Overview


I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list.

C# Solutions


Solution 1 - C#

Like this:

if (list.Distinct().Skip(1).Any())

Or

if (list.Any(o => o != list[0]))

(which is probably faster)

Solution 2 - C#

I created simple extension method mainly for readability that works on any IEnumerable.

if (items.AreAllSame()) ...

And the method implementation:

    /// <summary>
    ///   Checks whether all items in the enumerable are same (Uses <see cref="object.Equals(object)" /> to check for equality)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="enumerable">The enumerable.</param>
    /// <returns>
    ///   Returns true if there is 0 or 1 item in the enumerable or if all items in the enumerable are same (equal to
    ///   each other) otherwise false.
    /// </returns>
    public static bool AreAllSame<T>(this IEnumerable<T> enumerable)
    {
        if (enumerable == null) throw new ArgumentNullException(nameof(enumerable));

        using (var enumerator = enumerable.GetEnumerator())
        {
            var toCompare = default(T);
            if (enumerator.MoveNext())
            {
                toCompare = enumerator.Current;
            }

            while (enumerator.MoveNext())
            {
                if (toCompare != null && !toCompare.Equals(enumerator.Current))
                {
                    return false;
                }
            }
        }

        return true;
    }

Solution 3 - C#

My variant:

var numUniques = 1;
var result = list.Distinct().Count() == numUniques;

Solution 4 - C#

This is an option, too:

 if (list.TrueForAll(i => i.Equals(list.FirstOrDefault())))

It is faster than if (list.Distinct().Skip(1).Any()) , and performs similarly as if (list.Any(o => o != list[0])), however, the difference is not significant, so I suggest using the more readable one.

Solution 5 - C#

VB.NET version:

If list.Distinct().Skip(1).Any() Then

Or

If list.Any(Function(d) d <> list(0)) Then

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaif KhanView Question on Stackoverflow
Solution 1 - C#SLaksView Answer on Stackoverflow
Solution 2 - C#MSkutaView Answer on Stackoverflow
Solution 3 - C#SevaView Answer on Stackoverflow
Solution 4 - C#KungFuryView Answer on Stackoverflow
Solution 5 - C#jorView Answer on Stackoverflow