Skip to content

Commit

Permalink
problem: new problem solution - 287 . Find the Duplicate Number
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Sep 20, 2023
1 parent 1bc7080 commit 629996e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 1631 | [Path With Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/) | Algorithms | [TypeScript](./problems/algorithms/pathWithMinimumEffort/PathWithMinimumEffort.ts) | Medium |
| 319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | Algorithms | [TypeScript](./problems/algorithms/bulbSwitcher/BulbSwitcher.ts) | Medium |
| 1584 | [Min Cost to Connect All Points](https://leetcode.com/problems/min-cost-to-connect-all-points/) | Algorithms | [TypeScript](./problems/algorithms/minCostToConnectAllPoints/MinCostToConnectAllPoints.ts) | Medium |
| 287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | Algorithms | [TypeScript](./problems/algorithms/findTheDuplicateNumber/FindTheDuplicateNumber.ts) | Medium |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Source : https://leetcode.com/problems/find-the-duplicate-number/
// Author : squxq
// Date : 2023-09-20

import { findDuplicate } from "./FindTheDuplicateNumber"; // Replace with the actual path to your code file

describe("findDuplicate", () => {
it("Test Case 1", () => {
const nums = [1, 3, 4, 2, 2];
expect(findDuplicate(nums)).toBe(2);
});

it("Test Case 2", () => {
const nums = [3, 1, 3, 4, 2];
expect(findDuplicate(nums)).toBe(3);
});

it("Test Case 3 (Minimum Input Size)", () => {
const nums = [1];
expect(findDuplicate(nums)).toBe(1);
});

it("Test Case 4 (Maximum Input Size)", () => {
const n = 100000;
const nums = Array.from({ length: n }, (_, i) => i + 1);
nums.push(99999); // Add a duplicate
expect(findDuplicate(nums)).toBe(99999);
});

it("Test Case 5 (Duplicate at the Beginning)", () => {
const nums = [2, 2, 3, 4, 5];
expect(findDuplicate(nums)).toBe(2);
});

it("Test Case 6 (Duplicate at the End)", () => {
const nums = [1, 2, 3, 4, 5, 5];
expect(findDuplicate(nums)).toBe(5);
});

it("Test Case 7 (Duplicate in the Middle)", () => {
const nums = [1, 2, 3, 3, 4, 5];
expect(findDuplicate(nums)).toBe(3);
});

it("Test Case 8 (Random Order)", () => {
const nums = [5, 2, 1, 3, 4, 4];
expect(findDuplicate(nums)).toBe(4);
});

it("Test Case 9 (Duplicates at Both Ends)", () => {
const nums = [1, 2, 1, 3, 4, 5, 5];
expect(findDuplicate(nums)).toBe(1);
});

it("Test Case 10 (All Elements Are the Same)", () => {
const nums = [7, 7, 7, 7, 7];
expect(findDuplicate(nums)).toBe(7);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Source : https://leetcode.com/problems/find-the-duplicate-number/
// Author : squxq
// Date : 2023-09-20

/*****************************************************************************************************
*
* Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n]
* inclusive.
*
* There is only one repeated number in nums, return this repeated number.
*
* You must solve the problem without modifying the array nums and uses only constant 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
* All the integers in nums appear only once except for precisely one integer which appears
* two or more times.
*
* Follow up:
*
* How can we prove that at least one duplicate number must exist in nums?
* Can you solve the problem in linear runtime complexity?
******************************************************************************************************/

// number[] -> number
// return the repeated number in the given an array of integers n + 1 integers where each integer is in the range [1, n]

/** Stub:
function findDuplicate(nums: number[]): number { return 0 }
*/

/** Wrong Implementation of Floyd's Tortoise and Hare Algorithm
function findDuplicate(nums: number[]): number {
if (nums.length < 3) return nums[0]
let d: number = 1, h: number = 2
while(nums[d] !== nums[h]) {
if (d === nums.length - 1) d = 0
else if (h + 2 > nums.length - 1) h = nums.length - h - 2
d++
h += 2
}
return nums[d]
};
*/

export function findDuplicate(nums: number[]): number {
let slow: number = nums[0] as number;
let fast: number = nums[0] as number;

do {
slow = nums[slow] as number;
fast = nums[nums[fast] as number] as number;
} while (slow !== fast);

let ptr1: number = nums[0] as number;
let ptr2: number = slow;
while (ptr1 !== ptr2) {
ptr1 = nums[ptr1] as number;
ptr2 = nums[ptr2] as number;
}

return ptr1;
}

0 comments on commit 629996e

Please sign in to comment.