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

Function cyclicSort

javascript/0287-find-the-duplicate-number.js:151–171  ·  view source on GitHub ↗
(nums, index = 0)

Source from the content-addressed store, hash-verified

149};
150
151const cyclicSort = (nums, index = 0) => {
152 const swap = (arr, a, b) => ([arr[a], arr[b]] = [arr[b], arr[a]]);
153
154 while (index < nums.length) {
155 /* Time O(N) */
156 const [num, arrayIndex, arrayNum] = [
157 nums[index],
158 nums[index] - 1,
159 nums[nums[index] - 1],
160 ];
161
162 const canSwap = !isSame(num, arrayNum);
163 if (canSwap) {
164 swap(nums, index, arrayIndex);
165
166 continue;
167 }
168
169 index++;
170 }
171};
172const isSame = (a, b) => a === b;
173
174const search = (nums) => {

Callers 1

findDuplicateFunction · 0.70

Calls 2

isSameFunction · 0.70
swapFunction · 0.70

Tested by

no test coverage detected