Find the Duplicate Number
Updated
Problem statement
You are given an integer array nums of length n + 1. Every value is in 1..n. Exactly one distinct value occurs more than once; it may occur more than twice. Return that value.
For this exercise, assume the conventional duplicate-number contract: do not modify the array, and aim for O(n) time and O(1) auxiliary space.
Function
findDuplicate(nums: int[]) → intExamples
Example 1
nums = [1,3,4,2,2]return = 2Cycle entry differs from first value.
Example 2
nums = [3,1,3,4,2]return = 3Cycle-entry duplicate near the start.
Example 3
nums = [1,1]return = 1Minimum n equals one.
Constraints
1 <= n <= 1000.nums.length = n + 1and1 <= nums[i] <= n.- Exactly one distinct value is repeated; all other values occur at most once.
More Walmart Labs Onsite Interview questions
Similar questions at other companies
- Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions