Press "Enter" to skip to content

Only Int Occurring an Even Number of Times in an Array

Ajk 0

Today I thought I would take a look at a small interview question with a small gimmick:
In an unsorted array of numbers that occurs an even number of times except one that occurs an odd number of times, find the number that occurs an even number of times.

Turns out we can do the usual Sorting and Hash Table with the usual complexity explained and then our last solution uses one property that only numbers occurring twice have. XORing a number with itself an even number of times will result in a 0!

How To Find the Only Int Occurring an Even Number of Times in an Array – Sorting

How To Find the Only Int Occurring an Even Number of Times in an Array – Hashing

How To Find the Only Int Occurring an Even Number of Times in an Array – XOR

Food for thought: What happens if 0 appears an odd number of times?
Hope you guys enjoyed it… and I’ll see you guys next time ;D

The following two tabs change content below.
If you like one of my posts the best way to support is give it a thumbs up, comment, or share it on social media 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *