Are All Elements Equal?
Challenge
Given a list of integers >= 0 , check if all of them are equal.
Tests
[1,1,1,1,1] -> true
[0,1,1,6,7] -> false
[1] -> true
[] -> undefined(you do not need to handle this)
You may take the integers in any form(strings, list, codepoints, etc.)
You can output two distinct values for true or false, or simply a truthy or falsy value in your language.
Trivial builtins are allowed, but it is encouraged to edit them into the trivial builtins answer, or try a different approach.
Zsh, 8 bytes >$@ <^$ …
3y ago
Japt, 3 bytes e¡g Tr …
3y ago
[APL (Dyalog Unicode)], 3 byte …
3y ago
Rockstar, 76 bytes Takes in …
3y ago
[Python 3], 20 bytes …
3y ago
[Haskell], 16 bytes …
3y ago
[C (gcc)], 46 bytes …
3y ago
[Ruby], 15 bytes ```ruby - …
3y ago
[brainfuck], 40 bytes Outpu …
3y ago
JavaScript, 20 bytes Output …
3y ago
J, 4 bytes ``` -:|. ``` …
3y ago
Ruby, 14 bytes ```ruby ->{ …
3y ago
[Jelly], 1 byte E Tr …
3y ago
Trivial answers [Husk], 1 b …
3y ago
J, 5 char ``` 2>#. ``` …
2y ago
MATL, 4 bytes ``` tPX= ``` …
3y ago
16 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
JavaScript, 20 bytes
Outputs false
for truthy and true
for falsey.
a=>a.some(x=>x-a[0])
Outputs true
and false
as normal.
a=>new Set(a).size<2
0 comment threads
Zsh, 8 bytes
>$@
<^$1
Outputs via exit code: 0
for not all the same; 1
for all the same
-
>$@
: create a file named for each element in the input- effectively deduplicates because you can only have one file with a given name
-
<^$1
: try to find a file with any name other than the first input. If there were at least two distinct inputs, this will succeed and return0
, otherwise it will fail with status1
0 comment threads
APL (Dyalog Unicode), 3 bytes
⍋≡⍒
⍋
computes the permutation vector that would sort the argument into ascending order
⍒
computes the permutation vector that would sort the argument into descending order
If they are identical (≡
), all elements must be equal.
See also this talk by Conor Hoekstra.
0 comment threads
Ruby, 15 bytes
->a{a.all?a[0]}
->a{ } # lambda taking array `a`
a.all? # do all items in the array match...
a[0] # ...the first?
0 comment threads
C (gcc), 46 bytes
f(int*a){return *a<0|a[1]<0||*a==a[1]&f(a+1);}
This takes an array that is terminated by a negative number (which is not part of the list content, just like the terminating \0
is not part of the string content in C strings).
0 comment threads
J, 4 bytes
-:|.
Checks if the input array matches itself reversed. This is a tacit form and is executed monadically as x f (g x)
.
Ruby, 14 bytes
->{_1|[]in[_]}
Alternative solution:
->a{!(a|a)[1]}
Couple of other solutions I developed:
->a{!a.uniq[1]} # 15 bytes
->{!(_1|[])[1]} # 15 bytes
->a{(a|a).one?} # 15 bytes
0 comment threads
Jelly, 1 byte
E
Boring builtin answer
Non builtin, the shortest I can get is 3 bytes:
ḷ\⁼
Here, we replace each element with the first element, then check if that is equal to the original array
0 comment threads
J, 5 char
2>#~.
How it works:
'~.' produces list of unique elements of list on its right
'#' counts how many elements in the list on its right
'2>' tests if number on its right only has one unique element
Sample runs:
2>#~. 3 3 3 4
0
2>#~. 3 3 3 3
1
0 comment threads
MATL, 4 bytes
tPX=
tPX=
t - duplicate with implicit input
P - fliplr in MATLAB, reverses top of stack
X= - isequal
2 comment threads