Challenges

+9
−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?
Why should this post be closed?

Trivial builtins (1 comment)

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

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

Why does this post require moderator attention?

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

+3
−0

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


Try it online!

Why does this post require moderator attention?

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

+3
−0

# Python 3, 20 bytes

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


Try it online!

Why does this post require moderator attention?

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

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

Why does this post require moderator attention?

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

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

+1
−0

## Husk, 1 byte

E


Try it online!

Why does this post require moderator attention?

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

# Jelly, 1 byte

E


Try it online!

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?

+0
−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).

Why does this post require moderator attention?

+0
−0

# Ruby, 14 bytes

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


Try this online!

Couple of other bytes solutions I developed:

->a{!a.uniq[1]}   # 15 bytes
->{!(_1|[])[1]}   # 15 bytes
->a{(a|[]).one?}  # 16 bytes

Why does this post require moderator attention?

Featured
Hot Posts
Challenges — "Hello, World!"
Challenges — Coat of Many Colours
Challenges — Cumulative Counts

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