Description :

Given a string of space separated binary digits or space separated booleans or an array of binary digits or array of booleans . Your job is to find the xor of each until you end up with one answer either 0 or 1. The inputs will always be valid and will only be either 0 or 1.

Example :

1 0 0 1 0 --> 0
1 0 1 1 1 0 0 1 0 0 0 0 --> 1


This is code golf so shortest code will win. Good luck.

• Your job is to find the xor of each until you end up with one answer either 0 or 1 Can you clarify a bit more what that means? – Luis Mendo Apr 13 '18 at 13:44
• given a string find the xor of first two then the xor of next two and so on. keep at it till you end with one digit. – Muhammad Salman Apr 13 '18 at 13:45
• Use the Sandbox! That's 3 challenges in a row that you propose that have troubles. Go to the sandbox, expose your challenge and update it based on community feedback. Then only post it on this site. – Olivier Grégoire Apr 13 '18 at 13:49
• Hi, I've downvoted this question because it is very trivial. Most answers are going to be one or two variations, with very little room for creative golfing. – AdmBorkBork Apr 13 '18 at 14:07
• Can the list of booleans be empty ? – Ton Hospel Apr 13 '18 at 14:12

JavaScript (ES6), 18 bytes

s=>eval(s.join^)


Try it online!

Java (JDK 10), 12 bytes

s->s.sum()%2


Try it online!

If a string is really required, then 20 bytes:

s->s.chars().sum()%2


Try it online!

The "string" answer uses the fact that a space is codepoint 32, which mod 2 returns 0.

• Wow. I feel kinda stupid now ;) – O.O.Balance Apr 13 '18 at 15:29

Python 2, 17 bytes

lambda a:sum(a)%2


Try it online!

• This answer is not valid. given a string of space separated binary digits. and not an array – Muhammad Salman Apr 13 '18 at 13:41
• Also may I ask why did you downvote the question ? is there something wrong – Muhammad Salman Apr 13 '18 at 13:41
• @MuhammadSalman ... I guess TFeld didn't downvote it. How do you know that? – user202729 Apr 13 '18 at 13:42
• You can rollback to your original answer, the spec has been changed to allow arrays – caird coinheringaahing Apr 13 '18 at 13:51
• @cairdcoinheringaahing, Thanks :) – TFeld Apr 13 '18 at 13:53

05AB1E, 2 bytes

OÉ


Try it online.

Explanation:

• O: Take the sum of the input-array
• É: Evaluates sum % 2 == 1, returning 1 if the sum is odd, 0 otherwise

3 bytes:

A leading Ç can be added if the space-delimited string input was still mandatory, instead of a boolean-array.

Try it online.

• Ç: Push the ASCII values of all characters, and implicitly convert it to a list. 0 1 would become [48, 32, 49] in that case. The O (sum) and É (is odd?) will still act the same.

JavaScript (Node.js), 23 bytes

a=>a.reduce((c,d)=>c^d)


Try it online!

• Welcome to PPCG! – Martin Ender Apr 14 '18 at 12:04

SNOBOL4 (CSNOBOL4), 44 bytes

A	X =X + INPUT	:S(A)
OUTPUT =REMDR(X,2)
END


Try it online!

Perl 5-p040, 10 bytes

Assumes the input list can't be empty

$\^=0+$_}{


Try it online!

MATL, 3 2 bytes

so


Input can be a numeric vector of the form [1 0 0 1 0], or a string such as '10010' (thanks to @Giuseppe for noticing!).

Try it online!

Explanation

The code is so simple that it hardly needs an explanation, but here it goes.

s    % Implicit input: numeric vector (or string). Sum of the numbers. (For string
% input, the ASCII codes are summed. Character '1' is odd, '0' is even, and
% space is even too, so the parity is the same as with numeric vector input)
o    % Parity. Implicit display

• heck, this would work on a string, since space=32 and doesn't change the parity. – Giuseppe Apr 13 '18 at 14:01
• Oh, good idea! My previous version used U to convert from string to numeric vector, but indeed it can be removed – Luis Mendo Apr 13 '18 at 15:14

Java 10, 41 bytes

b->{var r=1<0;for(var c:b)r^=c;return r;}


The variable r is initially set to false, since we are XORing b[0] with it. Try it online here.

Add++, 8 bytes

L~,€Os2%


Try it online!

Jelly, 53 2 bytes

^/


Try it online!

-2 bytes thanks to user202729!

• OSḂ also works. – user202729 Apr 13 '18 at 13:43
• @user202729 So it does! Thanks! – caird coinheringaahing Apr 13 '18 at 13:44

Befunge-98 (FBBI), 8 bytes

#.~+2%#@


Try it online!

• You can use #q~+2% instead of #.~+2%#@ and output via exit code to save 2 bytes. (In TIO you need to expand the debug tab if you want to see the exit code) – MercyBeaucou Apr 21 '18 at 18:44

Japt, 2 bytes

r^


Try it

Does exactly what it says on the tin, reduce the array by XORing.

Cubix, 11 bytes

i?+<^<@Oa1<


Try it online!

It's been a while, so it feels good to write a Cubix answer!

Whispers v2, 38 bytes

> Input
> 2
>> ∑1
>> 3%2
>> Output 4


Try it online!

I feel like 3 answers is too many, but I wanted to get Whispers out again. I'm still working on an XOR approach

(Traditional) APL, 3 or 4 bytes

TIO, courtesy Adám

≠/⍎⎕


Analysis:

⎕ - Accept input.
⍎ - "unquote" it (if it's a quoted string, this will convert it to a numeric vector. If it's already a numeric vector, this is effectively a null op)
/ - reduction - apply the operator to the left to each successive item in the vector to the right
≠ - "not equal" - when applied strictly to boolean arguments, this is functionally identical to XOR (which is not implemented as a separate operator in APL)

If it may be assumed that the input will be a numeric vector instead of a quoted string, then the 'unquote' can be removed, saving one byte:

≠/⎕

• HI. Can you kindly provide a TIO link. Thanks – Muhammad Salman Apr 13 '18 at 13:53
• I've never been able to get quad-input in APL to work at TIO, that's why I didn't do so here. – Jeff Zeitlin Apr 13 '18 at 13:54
• Ah I see. Oh well. Btw you can update your answer, it will be more easier now – Muhammad Salman Apr 13 '18 at 13:55
• Input is essentially the APL session (except that ⎕ and default output happens in Output and ⍞ output happens in Debug). For convenience, you can define your workspace in Header, Code and Footer, but only Code is counted into the char/byte count: Try it online! – Adám Apr 13 '18 at 14:14
• @Adám - Thank you. I'm not sure why I was never able to get it to work. – Jeff Zeitlin Apr 13 '18 at 14:18

Ruby, 42 bytes

->s{s.split(' ').reduce(0){|a,b|a^b.to_i}}


Try it online!

APL (Dyalog Unicode), 2 bytesSBCS

Tacit prefix function.

≠/


Try it online!

/ is reduction and ≠ is XOR because XOR only gives 1 if its arguments are unequal.

Julia 0.6, 12 bytes

b->⊻(b...)


Try it online!

⊻ is the xor symbol, and b... distributes an array as individual elements before sending it to the xor function (since xor needs multiple arguments passed separately, not a single array argument).

A bit more interestingly, a version that accepts space separated (/comma-separated/unseparated) boolean values as a string, and returns the xor result :

Julia 0.6, 22 bytes

b->sum(Int.([b...]))%2


Try it online!

C (gcc), 4139 38 bytes

Saved a few bytes with inspiration from ceilingcat.

Saved another byte thanks to Jonathan Frech

r;f(char*s){for(r=0;*s;)r^=*s++;r&=1;}


Try it online!

• Can s=r&1 not be r&=1? – Jonathan Frech Aug 6 '18 at 3:52