Skip to content

Commit 5e0571f

Browse files
committed
add: moveElementToEnd function implementation
1 parent c5b3fae commit 5e0571f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Arrays/MoveElementToEnd.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { swap } from "./Swap";
2+
3+
/**
4+
* Moves all instances of a specified integer to the end of the array.
5+
*
6+
* @param {number[]} array - The input array of integers.
7+
* @param {number} toMove - The integer to be moved to the end.
8+
* @returns {number[]} - The modified array with the specified integer at the end.
9+
*
10+
* @description
11+
* Given an array of integers and an integer value, this function mutates the input array
12+
* by moving all instances of the specified integer to the end of the array while maintaining
13+
* the order of the other integers.
14+
*
15+
* @complexity
16+
* Time complexity:
17+
* - Best-case time complexity: O(n) - Occurs when there are no instances of the specified integer in the array, and the function performs a single pass through the array.
18+
* - Average-case time complexity: O(n) - In typical scenarios, the function iterates through the array once to move the specified integer to the end.
19+
* - Worst-case time complexity: O(n) - Happens when all elements in the array are the specified integer. The function still performs a single pass through the array.
20+
* Space complexity: O(1) - Constant space complexity, as the function performs the operation in place.
21+
*/
22+
function moveElementToEnd(array, toMove) {
23+
let left = 0;
24+
let right = array.length - 1;
25+
26+
while (left < right) {
27+
while (left < right && array[right] === toMove) {
28+
right--;
29+
}
30+
31+
if (array[left] === toMove) {
32+
// Swap the element at 'left' with the element at 'right'
33+
swap(array, left, right);
34+
}
35+
36+
left++;
37+
}
38+
39+
return array;
40+
}
41+
42+
export { moveElementToEnd };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { moveElementToEnd } from "../MoveElementToEnd";
2+
3+
describe("moveElementToEnd", () => {
4+
it("should move all instances of the specified integer to the end (best case)", () => {
5+
const array = [1, 2, 3, 4, 5];
6+
const toMove = 6;
7+
const expectedArray = [1, 2, 3, 4, 5];
8+
9+
const result = moveElementToEnd(array, toMove);
10+
11+
expect(result).toEqual(expectedArray);
12+
});
13+
14+
it("should move all instances of the specified integer to the end (average case)", () => {
15+
const array = [2, 1, 2, 2, 2, 3, 4, 2];
16+
const toMove = 2;
17+
const expectedArray = [4, 1, 3, 2, 2, 2, 2, 2];
18+
19+
const result = moveElementToEnd(array, toMove);
20+
21+
expect(result).toEqual(expectedArray);
22+
});
23+
24+
it("should move all instances of the specified integer to the end (worst case)", () => {
25+
const array = [3, 3, 3, 3, 3];
26+
const toMove = 3;
27+
const expectedArray = [3, 3, 3, 3, 3];
28+
29+
const result = moveElementToEnd(array, toMove);
30+
31+
expect(result).toEqual(expectedArray);
32+
});
33+
34+
it("should handle an empty array", () => {
35+
const emptyArray = [];
36+
const toMove = 5;
37+
38+
const result = moveElementToEnd(emptyArray, toMove);
39+
40+
expect(result).toEqual([]);
41+
});
42+
});

0 commit comments

Comments
 (0)