# Check if an array (or equivalent) has the same number of odd and even numbers - Code Golf Edition!

Inspired by this code review question, I am curious to see what happens if we turn that into a code-golf challenge!

The description is easy, the input is an array or any similar data structure with only unsigned integers, the output is a boolean if the number of odd numbers is equal to the number of even numbers inside (true or false doesn't matter, as long as the opposite is used in case the number doesn't match)

Here are some examples with true as output for matching (thanks to the original OP)

[5, 1, 0, 2]      ->  true
[5, 1, 0, 2, 11]  ->  false
[]                ->  true


Usual code-golf rules, shortest code wins.

• Challenges should be self contained. References to external resources are welcome, but you should not rely on them. Please post some test cases here. – manatwork May 22 at 10:39
• @ElPedro I could say no if I wanted to be strict but, why not, maybe something interesting comes out of it. I'll edit the question. – bracco23 May 22 at 12:31
• @ElPedro sorry not empty lists exemptions, you should always consider edge cases to avoid bugs :P – bracco23 May 22 at 12:36
• @DomenicoModica A similar assumption has been made for the C solution. Since the input is only unsigned numbers, using a sentinel of -1 for end I think is fair. Go ahead ;) – bracco23 May 22 at 22:24
• Does it need to be a Boolean or falsy/truthy is fine as usual? – Luca Citi May 22 at 23:57

# Racket, 44 bytes

(λ(a)(=(apply +(map(λ(x)(expt -1 x))a))0))


Try it online!

# Jelly, 4 bytes

-*S¬


Try it online!

Could be 3 bytes under "one consistent value, one non-consistent value" rules, with -*S outputting 0 for true and anything else for false.

   ¬    Whether or not 0 equals
S     the sum of
-       -1
*      to the power of every element of the input (implicitly vectorized).


# Clojure (60 bytes)

(defn q[c](reduce = (map #(count(filter % c))[odd? even?])))


Ungolfed:

(defn eq-odd-even [c]
(reduce = (map #(count (filter % c)) [odd? even?])))


Try it online!

# Octave, 19 bytes

@(x)(~sum((-1).^x))


Try it online!

# SQLite, 28 bytes

SELECT SUM(N%2*2-1)=0 FROM T


Inspired by this comment.

Try it online!

# [Excel], 28 bytes

Paste this into a cell outside of column A, the array goes in column A.

=0=SUM(IF((A:A<>""),-1^A:A))


An example:

Edit: shortened both by one byte by changing n%2*-2+1 to n%2*2-1

# JavaScript (Node.js), 28 bytes with -p flag

.map((i=0,n=>i+=n%2*2-1));!i


Try it online!

Link leads to a TIO instance with console.log(!i) because I can't find out how to use the Node.js -p flag on TIO.

# JavaScript (Node.js), 32 bytes

a=>(a.map(x=>i+=x%2*2-1),!i);i=0


Try it online!

This link also leads to a snippet with console.log because I still can't figure out how to use the -p flag, but this one's a function.

# Java (JDK), 28 bytes

a->a.map(n->-n%2|1).sum()==0


Try it online!

# Zpr'(h, 151 bytes

f|>\

(g ° (section p <\$> @))
(g .l)|>((2 * (length (filter (section 0 == @) l))) == (length l))
(p ())|>0
(p (S ()))|>1
(p (S (S .n)))|>(p n)
<|prelude.zpr

test-cases |> ('\
(' 5 (' 1 (' 0 (' 2 ())))) ('\
(' 5 (' 1 (' 0 (' 2 (' 11 ()))))) ('\
(' 2 (' 16 (' 3 (' 2 (' 11 (' 19 ())))))) ('\
(' 2 (' 16 (' 3 (' 4 (' 2 (' 11 (' 19 ()))))))) ('\
() \
())))))
main |> (zip test-cases (map f test-cases))

% ./Zprh stdlib/golf.zpr --de-peano
(' ((' 5 (' 1 (' 0 (' 2 0)))) , true) (' ((' 5 (' 1 (' 0 (' 2 (' 11 0))))) , false) (' ((' 2 (' 16 (' 3 (' 2 (' 11 (' 19 0)))))) , true) (' ((' 2 (' 16 (' 3 (' 4 (' 2 (' 11 (' 19 0))))))) , false) (' (0 , true) 0)))))