The Only Nonrepeating Array Element
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A Mygate OA reported in September 2023 hands you an array of up to 10^5 integers and asks for the one value that appears exactly once. Checking every element against every other is O(n^2), which is around 10 billion comparisons at the top of the range. That won't pass. It's a hash-table counting problem dressed up as an array question, and it's easy once you see it. If you blank during the live assessment, StealthCoder runs invisibly on your desktop and gives you the solution as a safety net.
The problem
Given a nonempty integer array nums, return its only value that occurs exactly once. The input contains exactly one such value. Every other distinct value occurs at least twice, and those repeated values may have different frequencies. The array is not necessarily sorted. Function onlyNonrepeatingElement(nums: int[]) → int Examples Example 1 nums = [4,1,4,2,1] return = 2 Only 2 occurs once. Example 2 nums = [-1,8,8,8,-1,-1,5] return = 5 The repeated values each occur three times; only 5 occurs once. Example 3 nums = [9] return = 9 A singleton has exactly one value occurring once. Constraints 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Exactly one value has frequency 1; every other distinct value has frequency at least 2.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the frequency map. One pass counts each value, a second pass returns the key whose count is 1. That's O(n) time and O(n) space, and it's the safe answer here. The pitfall is reaching for XOR. XOR only cancels values that appear exactly twice, and this problem says repeated values can show up three times or more, like the three 8s and three -1s in example 2. XOR would return garbage. Sorting works too, at O(n log n), by finding the element whose neighbors both differ. Watch the edge case: a single-element array returns that element. Values go up to 10^9 in magnitude, so use a hash map and not an array indexed by value. If you freeze on the live OA, StealthCoder is the hedge that reads the prompt and hands you the counting solution.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill The Only Nonrepeating Array Element cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
The Only Nonrepeating Array Element FAQ
What's the trick in The Only Nonrepeating Array Element?+
Count frequencies with a hash map, then return the value with count 1. One pass to build the map, one pass to find the answer. It runs in O(n) time, which handles 10^5 elements easily. Don't overthink it, the problem is a counting exercise.
Can I use XOR like in Single Number?+
No. XOR cancels pairs, but here repeated values can appear three or more times, as in the [-1,8,8,8,-1,-1,5] example. Three equal values XOR to themselves, so the result gets polluted. Stick with a hash map or sorting.
Why does brute force fail on this one?+
With n up to 10^5, comparing each element to all others means roughly 10^10 operations. That times out. A hash map brings it to about 10^5 operations, which is trivial.
What edge cases should I test?+
Test a single-element array like [9], which returns 9. Test negatives and large magnitudes up to 10^9, so use a map and not an index array. Test values repeated with different frequencies, like twos and threes mixed together.
How do I prepare for this in 48 hours?+
Write the frequency-map version from memory in your language of choice, then write the sort-based version as a fallback. Run the three given examples. This takes under an hour. Spend the rest of your time on other hash-table and counting patterns that Mygate-style OAs might pair with it.