This is the second in a series about programming interview questions and how to think through and solve them! Have you already solved this one? Check out all permutations of a set.
My first thought was to use an object to store each value as a key, then associate it with the count of each value. From there, we know we can’t just loop through the object because an object doesn’t keep order like an array.
We can then loop through the original array and check to see if the current value has an even number of occurrences within the object. If we find one, we’ll return that first value. If we don’t come across one, we have to return null.
The time complexity of the above algorithm should be the same as the number of items produced. Therefore, with an input size of n the time complexity is linear — O(n).
Nice job making it through this one! Work through the next one, all permutations of a set which is a very common one asked in advanced programming interviews.
Having worked across sites raking in over 50 billion website visits annually, Michael Mitrakos can help your business to crush the web game with an award winning website.
I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!