Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Are All Elements Equal?

+10
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Trivial builtins (2 comments)
CW trivial builtins answer? (5 comments)

16 answers

+0
−0

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

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

MATL, 4 bytes

tPX=

Try it online!

tPX=
t    - duplicate with implicit input
 P   - fliplr in MATLAB, reverses top of stack
  X= - isequal
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Ruby, 14 bytes

->{_1|[]in[_]}

Alternative solution:

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

Try this online!

Couple of other solutions I developed:

->a{!a.uniq[1]}   # 15 bytes
->{!(_1|[])[1]}   # 15 bytes
->a{(a|a).one?}   # 15 bytes
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 4 bytes

-:|.

Try it online!

Checks if the input array matches itself reversed. This is a tacit form and is executed monadically as x f (g x).

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

This requires parentheses to work, so shouldn't they be counted? (4 comments)
+2
−0

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

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? 

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Trivial answers

Husk, 1 byte

E

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

You don't need signum (4 comments)
+2
−0

brainfuck, 40 bytes

Outputs ÿ if all elements are equal, and \x00 byte otherwise.

->,>,[[-<->>+<]<[-<[-]>]>>[-<<+>>]<,]<<.

Try it online!

brainfuck, 41 bytes

Different approach. Output is opposite.

,[<+<,]>>[<[>->]<<[<<]>>>]<[>[[-]-.+<-]>]

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+4
−0

Japt, 3 bytes

e¡g

Try it

e¡g     :Implicit input of array U
e       :Is equal to
 ¡      :Map U
  g     :  First element of U
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Jelly, 1 byte

E

Try it online!

Boring builtin answer

Non builtin, the shortest I can get is 3 bytes:

ḷ\⁼

Try it online!

Here, we replace each element with the first element, then check if that is equal to the original array

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Rockstar, 76 bytes

Takes individual integers as input.

listen to f
let n be f
o's1
while n
let o be o and n is f
listen to n

say o

Try it (Code and input will need to be added manually, with each input integer on its own line)

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

JavaScript, 20 bytes

Outputs false for truthy and true for falsey.

a=>a.some(x=>x-a[0])

Try it online!

Outputs true and false as normal.

a=>new Set(a).size<2

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+4
−0

APL (Dyalog Unicode), 3 bytes

⍋≡⍒

Try it online!

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Python 3, 20 bytes

lambda l:len({*l})<2

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Haskell, 16 bytes

f(a:b)=all(==a)b

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+6
−0

Zsh, 8 bytes

>$@
<^$1

Attempt This Online!

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 return 0, otherwise it will fail with status 1
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »