## DEV Community is a community of 603,019 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Discussion on: Coding Puzzles: Week of 4/8 Joshua Gilless

Thanks for this! I don't really know C++, but I figured I'd give it a shot:

``````int stray(std::vector<int> numbers) {
int n1, n2, n3;
for (int i = 2; i < numbers.size(); i++) {
n1 = numbers[i];
n2 = numbers[i - 1];
n3 = numbers[i - 2];
if (n1 == n2 && n1 != n3) {
return n3;
} else if (n1 == n3 && n1 != n2) {
return n2;
} else if (n1 != n2 && n2 == n3) {
return n1;
}
}
};
`````` Joshua Gilless • Edited

@laurieontech did something really cool in JS with a bitwise XOR (her answer is above), so I figured I'd update this C++ answer with a bitwise XOR since I love it!

``````int stray(std::vector<int> numbers) {
int r = numbers;
for (int i = 1; i < numbers.size(); i++)
{
r ^= numbers[i];
}
return r;
};
`````` russ

Looks a bit messy, but I was going for something that might not be too inefficient with a large input array

``````def stray(arr):
count = {}
for index, i in enumerate(arr):
count[i] = count.setdefault(i, 0) + 1
if index >= 2 and len(count.keys()) > 1:
break
return next(k for k, v in count.items() if v == 1)
`````` Jelle Bekker

in c# using linq:

``````public static int Stray(int[] numbers)
{
return numbers.Aggregate((x, y) => x ^ y);
}
``````

or:

``````public static int Stray(int[] numbers)
{
return numbers.GroupBy(x => x).Single(num => num.Count() == 1).Key;
}
`````` Ahmed Musallam • Edited

here my cheap solution:

``````function stray(array) {
var sorted = array.sort((a,b) => a - b)
if (sorted === sorted) return sorted.pop();
else return sorted.shift()
}
``````

or shorter and more unreadable:

``````function stray(array) {
var sorted = array.sort((a,b) => a - b)
return sorted === sorted ? sorted.pop() : sorted.shift();
}

`````` Courtney • Edited

This is the first I'm finding this, excited to play along! Here is yesterday's (edited to make the colors show up):

``````function stray(numbers) {
let num1 = numbers, num2;
let dupOf1 = false;
for(let i=1; i<numbers.length; i++) {
if(numbers[i] === num1) {
if(num2) return num2;
else dupOf1 = true;
}
else if(numbers[i] === num2) return num1;
else num2 = numbers[i];
}
return dupOf1 ? num2 : num1;
}
`````` Laurie • Edited

Haha, bitwise xor. Since it's immutable and commutative it'll reduce down to the stray!

I should add that this only works because it’s an odd number of elements in the array. An even number of matching elements cancel each other out to result in the “stray”.

Thread russ

Oh my, it even works in python!

``````def stray(arr):
count = {}
for index, i in enumerate(arr):
count[i] = count.setdefault(i, 0) + 1
if index >= 2 and len(count.keys()) > 1:
break
return next(k for k, v in count.items() if v == 1)

from functools import reduce

def intstray(arr: [int]):
return reduce(lambda x,y: x ^ y, arr)

assert(intstray([1, 1, 2]) == 2)
#nope..!
#assert(intstray([1, 1, 2, 1]) == 2)
assert(intstray([17, 17, 3, 17, 17, 17, 17]) == 3)
assert(intstray([1, 2, 2]) == 1)
assert(stray(["bob", "bob", "bob", "steve", "bob"]) == "steve")
``````
Thread