How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?

后端 未结 9 570
孤城傲影
孤城傲影 2020-12-02 10:41

If all the items in a list have the same value, then I need to use that value, otherwise I need to use an “otherValue”. I can’t think of a simple and clear way of doing this

相关标签:
9条回答
  • 2020-12-02 11:14

    Though you certainly can build such a device out of existing sequence operators, I would in this case be inclined to write this one up as a custom sequence operator. Something like:

    // Returns "other" if the list is empty.
    // Returns "other" if the list is non-empty and there are two different elements.
    // Returns the element of the list if it is non-empty and all elements are the same.
    public static int Unanimous(this IEnumerable<int> sequence, int other)
    {
        int? first = null;
        foreach(var item in sequence)
        {
            if (first == null)
                first = item;
            else if (first.Value != item)
                return other;
        }
        return first ?? other;
    }
    

    That's pretty clear, short, covers all the cases, and does not unnecessarily create extra iterations of the sequence.

    Making this into a generic method that works on IEnumerable<T> is left as an exercise. :-)

    0 讨论(0)
  • 2020-12-02 11:16
    var val = yyy.First().Value;
    return yyy.All(x=>x.Value == val) ? val : otherValue; 
    

    Cleanest way I can think of. You can make it a one-liner by inlining val, but First() would be evaluated n times, doubling execution time.

    To incorporate the "empty set" behavior specified in the comments, you simply add one more line before the two above:

    if(yyy == null || !yyy.Any()) return otherValue;
    
    0 讨论(0)
  • 2020-12-02 11:16

    A slight variation on the above simplified approach.

    var result = yyy.Distinct().Count() == yyy.Count();

    0 讨论(0)
  • 2020-12-02 11:25

    This may be late, but an extension that works for value and reference types alike based on Eric's answer:

    public static partial class Extensions
    {
        public static Nullable<T> Unanimous<T>(this IEnumerable<Nullable<T>> sequence, Nullable<T> other, IEqualityComparer comparer = null)  where T : struct, IComparable
        {
            object first = null;
            foreach(var item in sequence)
            {
                if (first == null)
                    first = item;
                else if (comparer != null && !comparer.Equals(first, item))
                    return other;
                else if (!first.Equals(item))
                    return other;
            }
            return (Nullable<T>)first ?? other;
        }
    
        public static T Unanimous<T>(this IEnumerable<T> sequence, T other, IEqualityComparer comparer = null)  where T : class, IComparable
        {
            object first = null;
            foreach(var item in sequence)
            {
                if (first == null)
                    first = item;
                else if (comparer != null && !comparer.Equals(first, item))
                    return other;
                else if (!first.Equals(item))
                    return other;
            }
            return (T)first ?? other;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:27
    public int GetResult(List<int> list){
    int first = list.First();
    return list.All(x => x == first) ? first : SOME_OTHER_VALUE;
    }
    
    0 讨论(0)
  • 2020-12-02 11:30

    If a array is of type multidimension like below then we have to write below linq to check the data.

    example: here elements are 0 and i am checking all values are 0 or not.
    ip1=
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0

        var value=ip1[0][0];  //got the first index value
        var equalValue = ip1.Any(x=>x.Any(xy=>xy.Equals()));  //check with all elements value 
        if(equalValue)//returns true or false  
        {  
        return "Same Numbers";  
        }else{  
        return "Different Numbers";   
        }
    
    0 讨论(0)
提交回复
热议问题