MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / findDuplicate

Function findDuplicate

javascript/0287-find-the-duplicate-number.js:7–19  ·  view source on GitHub ↗
(nums)

Source from the content-addressed store, hash-verified

5 * @return {number}
6 */
7var findDuplicate = function (nums) {
8 nums.sort(
9 (a, b) => a - b,
10 ); /* Time O(N * log(N)) | HeapSort Space O(1) | QuickSort Space O(log(N)) */
11
12 for (let i = 1; i < nums.length; i++) {
13 /* Time O(N) */
14 const isPrevDuplicate = nums[i - 1] === nums[i];
15 if (isPrevDuplicate) return nums[i];
16 }
17
18 return -1;
19};
20
21/**
22 * https://leetcode.com/problems/find-the-duplicate-number/

Callers

nothing calls this directly

Calls 12

getCountFunction · 0.85
calcMaxBitFunction · 0.85
negativeMarkingFunction · 0.85
restoreToPositiveNumbersFunction · 0.85
countFunction · 0.70
cyclicSortFunction · 0.70
searchFunction · 0.70
isSameFunction · 0.70
swapFunction · 0.70
moveFastFunction · 0.70
moveSlowFunction · 0.70
addMethod · 0.45

Tested by

no test coverage detected