Skip to content

Commit 45d4be7

Browse files
committed
Refactor calculateMedian to eliminate unnecessary array cloning before sorting
1 parent c6076ed commit 45d4be7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Sprint-1/fix/median.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function calculateMedian(list) {
1414

1515
if (numbers.length === 0) return null; // Must have at least one number
1616

17-
const sorted = [...numbers].sort((a, b) => a - b);
18-
// numeric array copied before sorting in ascending order so that original array not mutated
17+
const sorted = numbers.slice().sort((a, b) => a - b);
18+
// replaced unnecessary array clone
1919

2020
const mid = Math.floor(sorted.length / 2); // math.floor gives the correct index
2121
if (sorted.length % 2 === 1) {

0 commit comments

Comments
 (0)