Mid Coding

Find the single non-repeated element in an array where every other?

element appears twice

public int SingleNonRepeated(int[] nums) {

int result = 0;

foreach (int num in nums) {

result ^= num;

return result;

Explanation:

XOR of a number with itself is 0; XOR with 0 is the number. So duplicates cancel out,

leaving the unique number.

Follow on:

More from C# Programming Tutorial

All questions for this course