17
\$\begingroup\$

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
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Luis Mendo Apr 21 at 9:08
  • \$\begingroup\$ @LuisMendo Input and output can be taken/given in any reasonable and convenient format, that's pretty standard for more trivial challenges \$\endgroup\$ – Redwolf Programs Apr 21 at 12:50
  • 3
    \$\begingroup\$ @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? \$\endgroup\$ – Luis Mendo Apr 21 at 14:29

43 Answers 43

13
\$\begingroup\$

Trivial Built-in Answers

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.

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

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!

\$\endgroup\$
4
  • 2
    \$\begingroup\$ I think that any(diff(scan())) also works for 17 bytes \$\endgroup\$ – user2390246 Apr 21 at 11:07
  • \$\begingroup\$ @user2390246 feel free to post that as your own answer! \$\endgroup\$ – Giuseppe Apr 21 at 11:51
  • 2
    \$\begingroup\$ -2 bytes as per @Dominic's comment and your own golf :) \$\endgroup\$ – pajonk May 13 at 19:28
  • \$\begingroup\$ @pajonk ah, very nice! \$\endgroup\$ – Giuseppe May 13 at 20:23
8
\$\begingroup\$

Haskell, 16 bytes

-12 bytes thanks to Delfad0r.

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

Try it online!

Another 16 byter:

f(a:x)=x==(a<$x)

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 16 bytes \$\endgroup\$ – Delfad0r Apr 21 at 6:46
  • \$\begingroup\$ @Delfad0r thanks a lot, I don't know how I missed that :/. \$\endgroup\$ – ovs Apr 21 at 6:57
  • 3
    \$\begingroup\$ Pointfree 15 bytes \$\endgroup\$ – Delfad0r Apr 21 at 8:13
6
\$\begingroup\$

Python 3, 18 bytes

lambda x:len({*x})

Try it online!

-5 bytes thanks to @hyper-neutrino

I'm lucky.....

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – hyper-neutrino Apr 21 at 1:55
  • 1
    \$\begingroup\$ Also, set(x) is the same as {*x} \$\endgroup\$ – hyper-neutrino Apr 21 at 1:58
  • \$\begingroup\$ @hyper-neutrino thanks! \$\endgroup\$ – wasif Apr 21 at 2:05
  • 5
    \$\begingroup\$ You're not lucky, you're wasif \$\endgroup\$ – lyxal Apr 21 at 2:31
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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?
\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ !==>- given all are integer? \$\endgroup\$ – l4m2 Apr 21 at 5:12
  • \$\begingroup\$ @l4m2 Good idea, I forgot about that trick \$\endgroup\$ – Redwolf Programs Apr 21 at 12:31
4
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ The same also works in APL: ≢∪ \$\endgroup\$ – Bubbler Apr 21 at 1:54
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – caird coinheringaahing Apr 21 at 1:57
4
\$\begingroup\$

Excel, 18 bytes

=MAX(A:A)=MIN(A:A)

Input is in column A. The formula can be input anywhere not in column A.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ (λ(x)(apply = x)) You don't need the check for singleton, it works just fine without it. \$\endgroup\$ – Wezl Apr 21 at 14:44
  • \$\begingroup\$ @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? \$\endgroup\$ – MLavrentyev Apr 21 at 14:48
  • 1
    \$\begingroup\$ probably, but go ahead and take advantage of new features \$\endgroup\$ – Wezl Apr 21 at 14:50
3
\$\begingroup\$

Proton, 7 bytes

set+len

Try it online!

Outputs 1 for truthy and anything else for falsy.

\$\endgroup\$
3
\$\begingroup\$

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)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ :D Arn! Very nice! \$\endgroup\$ – lyxal Apr 21 at 1:54
3
\$\begingroup\$

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...

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ You cannot convice me that this is zsh and not befunge or some variant :p \$\endgroup\$ – Redwolf Programs Apr 21 at 12:49
  • \$\begingroup\$ @RedwolfPrograms It does look pretty arrow-full \$\endgroup\$ – pxeger Apr 21 at 12:51
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

PowerShell, 17 bytes

($args|gu).length

Try it online!

Outputs 1 for truthy and anything else for falsy.

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

JavaScript, 21 bytes

d=>/^(.)\1*$/.test(d)

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ – Falco Apr 22 at 8:35
  • \$\begingroup\$ @Falco No, /../.test is RegExp.prototype.test, lost relation to the RegEx \$\endgroup\$ – l4m2 Apr 22 at 8:47
  • \$\begingroup\$ you are right, I though it would capture the object, but without the lambda expression it won't \$\endgroup\$ – Falco Apr 22 at 11:12
2
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ simply outputting the size should work as per the other answers. \$\endgroup\$ – Razetime Apr 21 at 5:38
2
\$\begingroup\$

Red, 25 bytes

func[s][single? unique s]

Doesn't work in TIO - apparently single? has been added in the subsequent versions.

enter image description here

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`  
    ]
]
\$\endgroup\$
2
\$\begingroup\$

Ruby, 14 bytes

->a{!(a|a)[1]}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt, 3 bytes

Returns 1 for true >1 for false.

â l

Try it (includes all test cases)

\$\endgroup\$
1
  • \$\begingroup\$ Does fU work? \$\endgroup\$ – AZTECCO Apr 21 at 9:12
2
\$\begingroup\$

Scala, 14 bytes

_.toSet.size<2

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 13 bytes

$_=/^(.)\1*$/

Try it online!

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

\$\endgroup\$
2
\$\begingroup\$

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.

Older answer was more readable:

f=->a{a.uniq.size==1}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Razetime Apr 22 at 1:35
  • \$\begingroup\$ @Razetime Thank you for warning me; i've edited the answer here (and in the other posts). \$\endgroup\$ – Sony Santos Apr 22 at 2:35
  • \$\begingroup\$ 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) \$\endgroup\$ – Razetime Apr 22 at 3:19
2
\$\begingroup\$

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 
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 5 bytes

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

Try it online!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.