Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

16. Find the Duplicate Number

MediumDay 4 of 60Arrays & Hashing

The array nums has n + 1 integers, each between 1 and n inclusive, so at least one value repeats. Exactly one value is repeated (possibly many times). Return that value without modifying the array and using only O(1) extra space.

Example 1
Input: nums = [1,3,4,2,2]
Output: 2
Example 2
Input: nums = [3,1,3,4,2]
Output: 3
Constraints
  • 1 <= n <= 10^5
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • Only one value repeats, but it may repeat more than twice
Hints
Frequently asked at: Amazon, Microsoft, Google, Adobe, Paytm
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min