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

Dashboard
Notifications
Mark all as read
Challenges

Are All Elements Equal?

+8
−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.

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

CW trivial builtins answer? (5 comments)
Trivial builtins (1 comment)

12 answers

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

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

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)

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

0 comment threads

+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!

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

0 comment threads

+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!

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!

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

0 comment threads

+2
−0

Haskell, 16 bytes

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

Try it online!

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!

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)
+1
−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!

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

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 »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!