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

66 Answers 66

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\$ Commented Apr 21, 2021 at 2:43
  • \$\begingroup\$ @Makonede I don't know, because I didn't edit it in \$\endgroup\$ Commented Apr 21, 2021 at 2:44
10
\$\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\$ Commented Apr 21, 2021 at 1:55
  • 1
    \$\begingroup\$ Also, set(x) is the same as {*x} \$\endgroup\$ Commented Apr 21, 2021 at 1:58
  • \$\begingroup\$ @hyper-neutrino thanks! \$\endgroup\$ Commented Apr 21, 2021 at 2:05
  • 7
    \$\begingroup\$ You're not lucky, you're wasif \$\endgroup\$ Commented Apr 21, 2021 at 2:31
  • \$\begingroup\$ Beat me to it :) \$\endgroup\$ Commented May 8, 2022 at 16:09
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\$ Commented Apr 21, 2021 at 6:46
  • \$\begingroup\$ @Delfad0r thanks a lot, I don't know how I missed that :/. \$\endgroup\$ Commented Apr 21, 2021 at 6:57
  • 5
    \$\begingroup\$ Pointfree 15 bytes \$\endgroup\$ 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\$ Commented Apr 21, 2021 at 11:51
  • 2
    \$\begingroup\$ -2 bytes as per @Dominic's comment and your own golf :) \$\endgroup\$ Commented May 13, 2021 at 19:28
  • \$\begingroup\$ @pajonk ah, very nice! \$\endgroup\$ Commented May 13, 2021 at 20:23
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\$

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

Pari/GP, 19 bytes

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

Try it online!

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

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\$ Commented Oct 20, 2023 at 1:55
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\$ 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\$ 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\$ Commented Apr 21, 2021 at 14:50
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\$ 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\$ Commented Apr 22, 2021 at 3:19
4
\$\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, 2024 at 8:56
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\$ Commented May 19, 2022 at 16:45
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\$ 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\$

Golfscript, 4 bytes

~.&, 

For the input: a set of numbers should be placed in '[]' without commas.

For the output: 1 is a truthy value while integers greater than 1 are falsey.

Try it online!

Explanation:

I hope this does not count as a 'trivial solution'... :)

~ # Evals the input to generate a list. . # Duplicates the list. & # Does setwise AND. Doing AND on two same lists will have the effect of removing duplicate elements. , # Returns the length of the remaining list. 
\$\endgroup\$
3
\$\begingroup\$

Mascarpone, 25 bytes

,['0.]v*1['1.]v*']<[:,>!]v*' <:' >,<:,>! 

Try It Online!

This program accepts strings similar to this one:

"[2 8 1 6 4]" 

and prints either '1' or '0' to stdout, with no newline.

Explanation

,['0.]v*1['1.]v*']<[:,>!]v*' <:' >,<:,>! ,['0.]v*1 First we take and ignore the first input character '[', and create an interpreter for which the default operation is to print ['1.]v*']< '0'. Next we bind the operation "print '1'" to the symbol ']' under this interpreter. [:,>!]v* Now we push an operation that takes a symbol from stdin, extracts the operation associated with that symbol from the interpreter on the stack, and executes it. ' < We now bind this operation to the space :' > character. Immediately, we extract the operation back out of the interpreter, and ,< bind it to the next symbol from stdin, which is the first number in the list. Finally, we :,>! manually execute the function once, to begin the cycle. Now, the interpreter will continue to take input until it receives a symbol that is neither space nor the first number. The program will print '1' if that symbol is ']', otherwise it will print '0'. 

I wrote a solution in 36 characters, however the input/output format is inconvenient enough that I don't think it qualifies:

,[$.]v*1['1.]v*']<[:,$,>!]v*,<:,$,>! 

It takes strings similar to this:

"[2 8 1 6 4 ]" 

and prints '1' if the items are all equal, and '[' otherwise.

As Mascarpone is character-set-agnostic (provided the set contains the 17 symbols used by the initial interpreter) and only 26 unique characters are needed for this program to succeed (the 17 original instructions, the space character, and the characters 2-9), we require only 5 bits per character. By this logic, the solutions would be 25 bytes and 22.5 bytes in size, respectively.

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

TI-Basic, 5 bytes

min(Ans=min(Ans 

Takes input in Ans. Outputs 0 for falsey, and 1 for truthy.

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

AWK, 18 bytes

1,$0=NF~gsub($1,e)

Attempt This Online!

Revisiting my former answer, I realized that if I wanted $0 to be printed every time, I can use range patterns. In fact, if begpat (the left part) is always true, then the range pattern is always true. So I substituted the appending ( )e technique to the range pattern 1,, saving one byte.

AWK, 19 bytes (former answer)

$0=(NF~gsub($1,e))e 

Try it online!

In details:

 gsub($1,e) Searches and substitutes every occurrence of the first number of the input ($1) for a null variable (e). It returns the number of substitutions made. NF~ Matches the Number of Fields (i.e., number of items) and the number of substitutions. We expect 1 or 0 as result, but there will be no output when it parses as 0. (AWK skips falsey patterns, and 0 is falsey.) ( )e Appending a null variable converts 1 to "1" and 0 to "0". That's good, because strings are always true. $0= Sets the original input to the resulting "1" or "0". As the pattern is a string, it is printed. 
\$\endgroup\$
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\$ Commented Oct 4, 2022 at 18:52
  • \$\begingroup\$ Or by using Julia's output method, remove =1 entirely \$\endgroup\$ Commented Oct 4, 2022 at 18:59
  • \$\begingroup\$ Good catch, I read that specification, but it didn't click. \$\endgroup\$ Commented Oct 4, 2022 at 21:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.