GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/problems/find-the-duplicate-number at master · ecgan/leetcode

Expand file tree

/

find-the-duplicate-number

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

find-the-duplicate-number

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
findDuplicate.js
findDuplicate.js
 
 
findDuplicate.test.js
findDuplicate.test.js
 
 

README.md

Outline

Find the Duplicate Number

LeetCode #: 287

Difficulty: Medium

Topics: Array, Two Pointers, Binary Search.

Problem

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Example 1:

Input: [1,3,4,2,2]
Output: 2

Example 2:

Input: [3,1,3,4,2]
Output: 3

Note:

  • You must not modify the array (assume the array is read only).
  • You must use only constant, O(1) extra space.
  • Your runtime complexity should be less than O(n^2).
  • There is only one duplicate number in the array, but it could be repeated more than once.

Solution Explanation

Reference:

  • My easy understood solution with O(n) time and O(1) space without modifying the array. With clear explanation. by echoxiaolee

  • https://keithschwarz.com/interesting/code/?dir=find-duplicate

  • Cycle Detection on Wikipedia (algorithm by Robert W. Floyd / Donald Knuth)

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.