Find the Value Occurring Exactly Once
Updated
Problem statement
Given an integer array nums, return the value that occurs exactly once. Exactly one distinct value has frequency one. Every other distinct value occurs at least twice, with no restriction on whether its frequency is odd or even.
For this exercise, inspect the input array in one left-to-right pass and leave it unchanged. You may store frequency counts and inspect those stored records after the array pass. Return the value rather than its index. No parity assumption is made.
Function
singleOccurrence(nums: int[]) → intExamples
Example 1
nums = [1,2,3,2,3,3,3,3,3,3,3,3,4,4,4,5,5]return = 1The frequencies are 1:1, 2:2, 3:9, 4:3, and 5:2. Only value 1 appears once. Raw XOR of the array is 6, demonstrating why an unreported even-frequency promise cannot be assumed.
Example 2
nums = [6,6,6,6,0,9,9,9]return = 0Zero occurs once, 6 occurs four times, and 9 occurs three times. The answer is 0. An odd frequency such as three does not make a value eligible.
Constraints
- For this exercise, assume 1 <= nums.length <= 5000.
- For this exercise, assume 0 <= nums[i] <= 1000000.
- Exactly one distinct value occurs once; every other distinct value occurs at least twice.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Find the Two Unique NumbersZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
- Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
- Largest Combination With Bitwise AND Greater Than ZeroMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Game of Life: In-Place Next GenerationGoogle · ONSITE INTERVIEWGoogle interview questions
- Single Number with TriplicatesGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- 2048 Simulation and Board CodecCitadel · ONSITE INTERVIEWCitadel interview questions
- SubsetsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- BitFont - Maximum Non-Overlapping KerningStripe · ONSITE INTERVIEWStripe interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)