28
\$\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
    Commented Apr 21, 2021 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\$ Commented Apr 21, 2021 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
    Commented Apr 21, 2021 at 14:29

63 Answers 63

14
\$\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!

Haskell + hgl, 2 bytes

lq

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.

Thunno, \$2 \log_{256}(96) \approx\$ 1.65 bytes

ze

Attempt This Online!

Thunno 2, 1 byte

Attempt This Online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why 05AB1E (legacy) and not just 05AB1E? \$\endgroup\$
    – Makonede
    Commented Apr 21, 2021 at 2:43
  • \$\begingroup\$ @Makonede I don't know, because I didn't edit it in \$\endgroup\$
    – lyxal
    Commented Apr 21, 2021 at 2:44
10
\$\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
    Commented Apr 21, 2021 at 6:46
  • \$\begingroup\$ @Delfad0r thanks a lot, I don't know how I missed that :/. \$\endgroup\$
    – ovs
    Commented Apr 21, 2021 at 6:57
  • 5
    \$\begingroup\$ Pointfree 15 bytes \$\endgroup\$
    – Delfad0r
    Commented Apr 21, 2021 at 8:13
10
\$\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\$ Commented Apr 21, 2021 at 11:07
  • \$\begingroup\$ @user2390246 feel free to post that as your own answer! \$\endgroup\$
    – Giuseppe
    Commented Apr 21, 2021 at 11:51
  • 2
    \$\begingroup\$ -2 bytes as per @Dominic's comment and your own golf :) \$\endgroup\$
    – pajonk
    Commented May 13, 2021 at 19:28
  • \$\begingroup\$ @pajonk ah, very nice! \$\endgroup\$
    – Giuseppe
    Commented May 13, 2021 at 20:23
9
\$\begingroup\$

Python 3, 18 bytes

lambda x:len({*x})

Try it online!

-5 bytes thanks to @hyper-neutrino

I'm lucky.....

\$\endgroup\$
5
  • 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
    Commented Apr 21, 2021 at 1:55
  • 1
    \$\begingroup\$ Also, set(x) is the same as {*x} \$\endgroup\$
    – hyper-neutrino
    Commented Apr 21, 2021 at 1:58
  • \$\begingroup\$ @hyper-neutrino thanks! \$\endgroup\$
    – Wasif
    Commented Apr 21, 2021 at 2:05
  • 7
    \$\begingroup\$ You're not lucky, you're wasif \$\endgroup\$
    – lyxal
    Commented Apr 21, 2021 at 2:31
  • \$\begingroup\$ Beat me to it :) \$\endgroup\$
    – Ohentis
    Commented May 8, 2022 at 16:09
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
    Commented Apr 21, 2021 at 5:12
  • \$\begingroup\$ @l4m2 Good idea, I forgot about that trick \$\endgroup\$ Commented Apr 21, 2021 at 12:31
5
\$\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\$

Proton, 7 bytes

set+len

Try it online!

Outputs 1 for truthy and anything else for falsy.

\$\endgroup\$
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
    Commented Apr 21, 2021 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\$ Commented Apr 21, 2021 at 1:57
4
\$\begingroup\$

Ruby, 14 bytes

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

Try it online!

\$\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
    Commented Apr 21, 2021 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\$ Commented Apr 21, 2021 at 14:48
  • 1
    \$\begingroup\$ probably, but go ahead and take advantage of new features \$\endgroup\$
    – Wezl
    Commented Apr 21, 2021 at 14:50
4
\$\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\$
4
\$\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
    Commented Apr 22, 2021 at 1:35
  • \$\begingroup\$ @Razetime Thank you for warning me; i've edited the answer here (and in the other posts). \$\endgroup\$ Commented Apr 22, 2021 at 2:35
  • 1
    \$\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
    Commented Apr 22, 2021 at 3:19
4
\$\begingroup\$

Pari/GP, 19 bytes

v(s)=gcd(s)==lcm(s)

Try it online!

\$\endgroup\$
4
\$\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\$
1
  • 2
    \$\begingroup\$ You cannot convice me that this is zsh and not befunge or some variant :p \$\endgroup\$ Commented Apr 21, 2021 at 12:49
4
\$\begingroup\$

K (ngn/k), 5 bytes

1=#?:

Try it online!

-2 bytes thanks to ngn

Returns 1 for true, 0 for false.

Checks if 1 is equal to the length of unique elements in x

\$\endgroup\$
1
  • \$\begingroup\$ trains are allowed here, so 1=#?: works too \$\endgroup\$
    – ngn
    Commented May 19, 2022 at 16:45
4
\$\begingroup\$

Uiua, 4 bytes

≅↻1.

Try it!

≅↻1.
   .  # duplicate
 ↻1   # rotate an array by one
≅     # do they match?
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Alternative 4 byters: =1⧻⊝ (length of deduplicated equals 1), ≅⊃⍏⍖ (sorting order and reverse sorting order are equal), ¬∊1⊛ (1 doesn't exist in classification) \$\endgroup\$
    – Bubbler
    Commented Oct 20, 2023 at 1:55
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
    Commented Apr 21, 2021 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\$

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\$
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\$

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\$
3
\$\begingroup\$

x86-16 machine code, 6 bytes

Hexdump:

0000:0000  89 F7 AC F3 AE C3                                ......

Explanation

                                   ; Routine A. [All The Same?]
                                   ; Expects CX = Length of input,
                                   ;         SI = Initial address of list.

0000:0000  89F7        MOV DI, SI  ; A1 [Load]. Set DI to SI.
0000:0002  AC          LODSB       ;            AL = [SI++].
0000:0003  F3AE        REPZ SCASB  ; A2 [Compare].
                                   ;       ZF = 1
                                   ;       Loop CX times:
                                   ;           If AX != [DI]:
                                   ;               ZF = 0
                                   ;               Proceed to A3
                                   ;           DI ++
0000:0105  C3          RET         ; A3 [End]. End of algorithm.
                                   ;           ZF = 0 if not all same.
                                   ;           ZF = 1 if all same. █
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$ Commented Apr 25, 2021 at 11:23
  • \$\begingroup\$ Nice, clever way to avoid off-by-one or a dec. Copying the pointer before lodsb makes repz scasb scan the whole array of length CX, including the first byte. The same machine code works in 32-bit mode (with ECX / ESI), or in 64-bit mode with 32-bit pointers. (Otherwise push rsi/pop rdi to copy a 64-bit register in 2 bytes.) \$\endgroup\$ Commented Jan 7 at 8:56
3
\$\begingroup\$

Fig, \$2\log_{256}(96)\approx\$ 1.646 bytes

LU

If you test input cases, the Fig site demands no spaces, ie. [x,y,z] not [x, y, z]. Outputs 1 if all equal, else >1.

Try it online!

LU
 U  : Uniquify with implicit input
L   : Length
\$\endgroup\$
3
  • \$\begingroup\$ You can replace =1 with { for inverted logical output \$\endgroup\$
    – Seggan
    Commented Oct 4, 2022 at 18:52
  • \$\begingroup\$ Or by using Julia's output method, remove =1 entirely \$\endgroup\$
    – Seggan
    Commented Oct 4, 2022 at 18:59
  • \$\begingroup\$ Good catch, I read that specification, but it didn't click. \$\endgroup\$
    – south
    Commented Oct 4, 2022 at 21:15
3
\$\begingroup\$

dc, 27 characters

?[0p3Q]sq[d3R!=qz0<c]dscx1p

Sample run:

bash-5.1$ dc -e '?[0p3Q]sq[d3R!=qz0<c]dscx1p' <<< '1 1 1 1 1 1 1'
1

bash-5.1$ dc -e '?[0p3Q]sq[d3R!=qz0<c]dscx1p' <<< '6 9 6 9 6'
0

Try all test cases online!
(Note that choosing dc as interpreter and running the test separately fails both in TIO and ATO. ☹)

\$\endgroup\$
1
  • \$\begingroup\$ ATO/TIO uses dc by running a file instead of -e. Why that makes a difference, I have no idea (I can reproduce locally), but it works with -e: ATO/TIO. \$\endgroup\$
    – naffetS
    Commented Oct 5, 2022 at 3:46
3
\$\begingroup\$

Ly, 6 bytes

a0I=u;

Try it online!

This one works by sorting the list of number and comparing the top and bottom of the stack.

a       - sort the stack, implicitly reads the numbers first
 0I     - copy the bottom entry to top of the stack
   =    - compare the top two entries
    u;  - print boolean result, exit to stop from auto printing the stack

jq, 14 bytes

unique[1]//"T"

Try it online!

This one generates a list of unique numbers, then attempts to print the second member of that list. The // operator catches the exception and substitutes T if there's only one number.

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

sed, 26 bytes

$!{N;/^\(.*\)\n\1$/D;Q1};Q

Try it online! - Takes input as a newline separated list of numbers, exits with a non-zero exit code for "false" (i.e. not a uniform list).

Explanation
$!{                    }     if not last line, do these
   N;                          append next input line to pattern space
     /^\(.*\)\n\1$/            if pattern space is two identical lines
                   D;            delete first line, and GOTO beginning
                     Q1        quit with exit code 1 (no printing)
                        ;Q   quit with exit code 0 (no printing)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf Stack Exchange, and nice first submission! \$\endgroup\$ Commented Oct 20, 2023 at 17:18
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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