This problem is in a manner an converse of How do you check if there are any equal arguments(even sublist) in a list? and is related to How do I check if any element in a list is positive? As with both of those there is a choice in approach of either scanning the entire light with a fast, vectorized operation, or providing for an early-exit behavior. Which one is desirable will depend on the most commonly tested expression form. Please review my answer to the second linked question for examples.
Using Tally
or Max
and Min
are examples of methods that scan the entire list without the possibility of an early exist.
The length of the list can also affect the performance of the methods used. For example, as noted here UnsameQ
works well on short lists (few arguments) but not on long ones. That doesn't appear to be the case with Equal
and SameQ
however so both of those are good general methods for your problem, and provide early exist behavior. You can choose between numeric and structural equivalence. kguler already showed these but repeated for completeness:
Equal @@ {1, 1, 1, 1}
SameQ @@ {1, 1, 1, 1}
True
True
Equal @@ {1, 1, 2, 1}
SameQ @@ {1, 1, 2, 1}
False
False
Equal @@ {1, 1, 1.0, 1}
SameQ @@ {1, 1, 1.0, 1}
True
False
To bring a unique method to this answer we can also use pattern matching for structural equivalence:
MatchQ[{1, 1, 1, 1}, {x_ ..}]
MatchQ[{1, 1, 2, 1}, {x_ ..}]
True
False
By naming the Pattern
(x_
) we restrict it to matching a particular expression. See:
Union
orDeleteDuplicates
and then if the resulting list has length greater than 1 then the elements are not all the same (not constant in your terminology) $\endgroup$