# Are All the Items the Same?

Given a list of non-negative integers, return whether or not that list is all the same number.

## Rules

• Input and output can be taken/given in any reasonable and convenient format
• Truthy/Falsey values can be represented as any value of your choice as long as it's reasonable and relatively consistent (e.g. 1 for falsey and >= 2 for truthy is fine)
• There will always be at least 1 item in the input list
• The list items are guaranteed to be in the range [0, 9] ($$\0 \le n \le 9\$$)
• Standard loopholes apply

This is code golf, so the shortest program in each language wins. I've made a community wiki answer for trivial answers, so feel free to submit potentially longer programs.

## Test Cases

[1, 1, 1, 1, 1, 1, 1] -> True
[1, 2, 3, 4] -> False
[6, 9, 6, 9, 6] -> False
[6] -> True
[7, 7] -> True
[4, 2, 0] -> False

• Relevant: Default policy for ouput in decision problems (since this challenge doesn't specify the allowed options for the output]. Anyway, it would be better for the challenge to explicitly specify what is allowed and what isn't – Luis Mendo Apr 21 at 9:08
• @LuisMendo Input and output can be taken/given in any reasonable and convenient format, that's pretty standard for more trivial challenges – Redwolf Programs Apr 21 at 12:50
• @RedwolfPrograms Sure, but I'm not talking about format. I understand format as outputting string '3' instead of number 3; or producing the output via program exit code. What I mean is what options for output are allowed: two consistent values? Non-consistent truthy/falsy? One consistent value for truthy and any inconsistent values for falsy? – Luis Mendo Apr 21 at 14:29

This is the post for all of the languages where this is a built-in

## Vyxal, 1 byte

≈


Try it Online!

# Jelly, 1 byte

E


Try it online!

# 05AB1E (legacy), 1 byte

Ë


Try it online!

# Wolfram Language (Mathematica), 5 bytes

Equal


Try it online!

SameQ also works.

# Brachylog, 1 byte

=


Try it online!

# Husk, 1 byte

E


Try it online!

# Factor, 3 bytes

std


Try it online!

Outputs 0.0 if they're the same, or something else if not. all-eq? and all-equal? are longer built-ins that output booleans. all-equal? uses = (ordinary equality) and all-eq? uses eq? (strict object reference equality), but they behave the same for integers.

• Why 05AB1E (legacy) and not just 05AB1E? – Makonede Apr 21 at 2:43
• @Makonede I don't know, because I didn't edit it in – lyxal Apr 21 at 2:44

# R, 17 15 bytes

sd(scan()+!1:2)


Try it online!

Outputs 0 for truthy and nonzero for falsey.

Using sd is a classic R golfing trick: the standard deviation is 0 if and only if all elements are equal. Unfortunately, sd returns NA for length one input (since it divides by n-1). A neat workaround found by pajonk uses R's recycling: !1:2 is coerced to a vector c(0,0) and is added to the input vector. A length-one input is recycled to be length 2 (so the sd is guaranteed to be 0), and for input of length more than 1, the zeros are recycled to the length of the longer vector, which won't change the standard deviation.

sd(rep(scan(),2))


Try it online!

• I think that any(diff(scan())) also works for 17 bytes – user2390246 Apr 21 at 11:07
• @user2390246 feel free to post that as your own answer! – Giuseppe Apr 21 at 11:51
• – pajonk May 13 at 19:28
• @pajonk ah, very nice! – Giuseppe May 13 at 20:23

f(a:x)=all(==a)x


Try it online!

Another 16 byter:

f(a:x)=x==(a<$x)  Try it online! • 16 bytes – Delfad0r Apr 21 at 6:46 • @Delfad0r thanks a lot, I don't know how I missed that :/. – ovs Apr 21 at 6:57 • Pointfree 15 bytes – Delfad0r Apr 21 at 8:13 # Python 3, 18 bytes lambda x:len({*x})  Try it online! -5 bytes thanks to @hyper-neutrino I'm lucky..... • You can remove the ==1 since it was agreed upon that outputting 1 for truthy and >1 for falsy was valid. Alternatively, it can be <2 since the input is guaranteed to have at least one element. – hyper-neutrino Apr 21 at 1:55 • Also, set(x) is the same as {*x} – hyper-neutrino Apr 21 at 1:58 • @hyper-neutrino thanks! – wasif Apr 21 at 2:05 • You're not lucky, you're wasif – lyxal Apr 21 at 2:31 # Vim, 8 bytes :g/<C-r><C-w>/d  Try it online! This uses the assumption that list elements are one character. Ctrl+RCtrl+W inserts the word under the cursor, so this solution applies the delete command to any line which contains the first element of the list. This results in an empty file if they're all the same, or some non-empty lines if they aren't. If we weren't allowed to assume that items are in the range [0,9] then we could give false positives on numbers which are supersets of each other. We could fix this by using a regex instead for one more byte: ### Vim, 9 bytes :%s/<C-r><C-w>\n  Try it online! Vim doesn't have a concept of truthy/falsey values but if one were to believe that a buffer containing only newlines was falsey, then we could drop the \n from this regex to get a 7 byte solution. # Jelly, 2 bytes IẸ  Try it online! Outputs reversed (0 if all are equal, 1 if not) ## How it works IẸ - Main link. Takes a list L on the left I - Increments of L Ẹ - Are any non-zero?  # JavaScript (non-trivial), 22 21 bytes Thanks to @l4m2 for -1 d=>!d.some(x=>x-d[0])  Try it online! This one's different from the new Set approach because it returns an actual truthy/falsy value. Although it's not as short (by just 4B, surprisingly), it's what you'd probably use if you needed this in real life, so it's definitely worth having here. You could make it a byte shorter by removing the !, but then it returns false if all items are the same and true otherwise. • !==>- given all are integer? – l4m2 Apr 21 at 5:12 • @l4m2 Good idea, I forgot about that trick – Redwolf Programs Apr 21 at 12:31 # Jelly, 2 bytes QL  Try it online! # Husk, 2 bytes Lu  Try it online! # 05AB1E, 2 bytes Ùg  Try it online! # APL(Dyalog Unicode), 2 bytes SBCS ≢∪  Try it on APLgolf! Thanks to Bubbler for pointing this out! Outputs 1 for truthy, $$\>1\$$ for falsey. These all use the same method: counting the number of unique elements. If the same approach is also this trivial/short in your language, feel free to edit it in if you don't want to post an answer • The same also works in APL: ≢∪ – Bubbler Apr 21 at 1:54 • @Bubbler I've edited that in. This is almost as trivial as the builtin, so I've edited in a note for other languages with the same approach – caird coinheringaahing Apr 21 at 1:57 # Excel, 18 bytes =MAX(A:A)=MIN(A:A)  Input is in column A. The formula can be input anywhere not in column A. # Racket, 37 18 bytes (λ(x)(apply = x))  Try it online! -19 thanks to Wezl This takes advantages of the fact that = in Racket can take any number of arguments. Note that the tio seems to run an old version that requires at least 2 arguments to = and so fails on the singleton list, but this works on my local Racket 8 installation. • (λ(x)(apply = x)) You don't need the check for singleton, it works just fine without it. – Wezl Apr 21 at 14:44 • @Wezl hmm you're right for my local isntallation, but tio for some reason throws an error on that. Perhaps they're running an old version? – MLavrentyev Apr 21 at 14:48 • probably, but go ahead and take advantage of new features – Wezl Apr 21 at 14:50 # Proton, 7 bytes set+len  Try it online! Outputs 1 for truthy and anything else for falsy. # Arn 1.0-s, 2 bytes :@  Outputs 1 if all the same, >1 if they aren't all the same. Try it online (works in older version) :@ groups identical values, -s takes size. This could be written as (:@)# as well, which would be 4 bytes («×+0) • :D Arn! Very nice! – lyxal Apr 21 at 1:54 # Branch, 27 bytes 1O,[^\,N![o#)]n^=/;^\o^*On]  Try it on the online Branch interpreter! There's got to be a much shorter way to do this... # Charcoal, 5 bytes ⁼⌊θ⌈θ  Try it online! Link is to verbose version of code. Takes input as a string of digits by default but you can feed it an array if you insist. Outputs a Charcoal boolean, i.e. - for equal, nothing if not. Explanation: Simply compares the minimum and maximum input element. # Retina 0.8.2, 7 bytes D. ^.$


Try it online! Link includes test cases. Takes input as a string of digits. Explanation:

D.


Remove duplicate digits.

^.$ Check that only one digit is left. # brainfuck, 40 bytes Works for all characters, not just 0 to 9! (except NUL) Returns zero for true and non-zero for false. ,[>,]<[->+<]<[[->+>-<<]>>[<[-]>>>]<<<]>.  Try it online! # Zsh-o extendedglob, 8 bytes >$@
<^$1  Try it online! Outputs via exit code; 1 for all the same, 0 for not all the same • >$@: create files according to the input. This de-duplicates elements because a file can only be created once
• ^$1: find a file that doesn't match the first element of the input (-o extendedglob is necessary to enable the ^ feature) • <: and try to read from it. If there is no matching file (because all elements are the same) this will fail and exit with 1 • You cannot convice me that this is zsh and not befunge or some variant :p – Redwolf Programs Apr 21 at 12:49 • @RedwolfPrograms It does look pretty arrow-full – pxeger Apr 21 at 12:51 # Pip, 3 bytes $=g


Full program; takes the items from command-line arguments. Try it online!

Alternately, a function solution that takes a list as its argument (also 3 bytes):

$=_  Try it online! ### Explanation Not quite a builtin. Pip, like Python, has chaining comparison operators: 1<5>=3 means 1<5 & 5>=3, and 1=1=1=2 means 1=1 & 1=1 & 1=2. Unlike Python, Pip also has the meta-operator $, which folds a list on a binary operator. For example, $+ folds a list on addition, returning the sum; and $= folds a list on =. Because = chains, this returns the result we want: 1 if all elements are equal and 0 otherwise. The program $=g applies this compound operator to the full arglist g. # V (vim), 10 bytes :sor u Gd{  Try it online! Input as a list of lines. Outputs a falsy value (Empty output) for all items equal, and outputs a number otherwise. Gets the unique lines, goes to the last line and deletes till the beginning. # PowerShell, 17 bytes ($args|gu).length


Try it online!

Outputs 1 for truthy and anything else for falsy.

• The script returns 1 for 1,1,1,1 and 3 for 1,2,1,1. Where is true/false? – mazzy Apr 21 at 4:50
• @mazzy see Hyper-neutrino's first comment under my python answer. This is perfectly allowed. – wasif Apr 21 at 4:51
• please add to description how you interpret to boolean – mazzy Apr 21 at 4:53
• @mazzy done that – wasif Apr 21 at 4:55

# JavaScript, 21 bytes

d=>/^(.)\1*$/.test(d)  Try it online! • In essence this is only 15 Bytes, since /../.test is already the whole function, since the parameter d is the only parameter to this function. – Falco Apr 22 at 8:35 • @Falco No, /../.test is RegExp.prototype.test, lost relation to the RegEx – l4m2 Apr 22 at 8:47 • you are right, I though it would capture the object, but without the lambda expression it won't – Falco Apr 22 at 11:12 # Raku, 7 6 bytes 2>*.Set +*.Set  Try it online! *.Set converts the input list into a set. That set's size is then compared with 2 returned. -1 byte thanks to Razetime • simply outputting the size should work as per the other answers. – Razetime Apr 21 at 5:38 # Red, 25 bytes func[s][single? unique s]  Doesn't work in TIO - apparently single? has been added in the subsequent versions. # Using parse, 54 bytes func[s][parse s[set t skip(r: reduce['quote t])any r]]  Try it online! Much longer, but a bit more interesting. ## Explanation: Red's parse works not only on strings, but on all types of series (block! paren! string! file! url! path! lit-path! set-path! get-path! vector! hash! binary! tag! email! image!). f: func[s][ parse s [ ; parse the input set t skip ; sets t to the first item ; a literal integer can't be used as a parse rule (r: reduce['quote t]) ; that's why set r to t's quoted value any r ; zero or more r ] ]  # Ruby, 14 bytes ->a{!(a|a)[1]}  Try it online! # Japt, 3 bytes Returns 1 for true >1 for false. â l  Try it (includes all test cases) • Does fU work? – AZTECCO Apr 21 at 9:12 # Scala, 14 bytes _.toSet.size<2  Try it online! # Perl 5-p, 13 bytes $_=/^(.)\1*\$/


Try it online!

Since the input is all single digit numbers, this wants the input without spaces.

# Ruby, 17 bytes

f=->a{!a.uniq[1]}


Explanation: a.uniq is an array with unique elements of a. If all elements are the same, its second element [1] will be nil, and !nil is true.

f=->a{a.uniq.size==1}

• According to the general input methods allowed on this site, you cannot assume the input is in a variable. You need to wrap this in a function or lambda for it to be valid. – Razetime Apr 22 at 1:35
• @Razetime Thank you for warning me; i've edited the answer here (and in the other posts). – Sony Santos Apr 22 at 2:35
• Usually, we omit f= in the submission unless is it reused somewhere else(or for recursion). Feel free to look at other ruby answers for examples. We generally use tio.run for hosting ruby solutions supported by version 2.5.5. (it also has a convenient submission generator for this site) – Razetime Apr 22 at 3:19

# C (gcc), 35 32 bytes

f(int*l){l=~l[1]&&*l-*++l+f(l);}


Try it online!

• recursive function taking an array -1 ended
• return False if all elements are equal, True otherwise
{l=      - return using eax trick
~l[1]&&  - return False if next item is end and skip next part ending recursion
*l-*++l  - 0 if different
+f(l);}  - plus check next item


# APL (Dyalog Unicode), 5 bytes

×/⊢=⊃
×/ ⍝ product reduce
⊢=⊃ ⍝ three train, each element equal to first element


Try it online!

• you can remove the parens for a 4-train. See (f g h k) here. – Razetime Apr 24 at 4:32
• @Razetime Oh, wow. Thanks :) – Andrew Ogden Apr 25 at 3:27
• alternatively ⍋≡⍒, ≠≡⍷⍨, or any of the solutions presented here youtube.com/watch?v=GZuZgCDql6g – rak1507 Apr 26 at 9:08
• @rak1507 I hadn't committed them to memory yet, this is just what came to my head. That was a great talk though. – Andrew Ogden Apr 26 at 14:18