-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.js
More file actions
87 lines (77 loc) · 2.06 KB
/
Copy pathArray.js
File metadata and controls
87 lines (77 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class MyArray {
constructor() {
this.length;
this.data = {};
}
get(index) {
return this.data[index];
}
push(item) {
this.data[this.length] = item;
this.length++;
return this.length;
}
pop() {
const lastItem = this.data[this.length - 1];
delete this.data[this.length - 1];
this.length--;
return lastItem;
}
delete(index) {
const item = this.data[index];
this.shiftItems(index);
return item;
}
shiftItems(index) {
for (let i = 0; i < index.length - 1; i++) {
this.data[i] = this.data[i + 1];
}
delete this.data[this.length - 1];
this.length--;
}
}
// Reverse a string problem
function reverseString(text) {
const textArray = text.split("");
const reversedArray = [];
for (let index = textArray.length - 1; index > -1; index--) {
const element = textArray[index];
reversedArray.push(element);
}
return reversedArray;
}
function reverseString2(text) {
return text.split("").reverse().join("");
}
function reverseString3(str) {
return [...str].reverse().join("");
}
reverseString("text");
// Merge sorted arrays problem
function mergeSortedArray(arr1, arr2) {
const newArray = [];
// Pointer for each element in each array
let firstPointer = 0;
let secondPointer = 0;
// Loop over the two arrays using pointers to compare values
while (firstPointer < arr1.length || secondPointer < arr2.length) {
if (arr1[firstPointer] < arr2[secondPointer]) {
newArray.push(arr1[firstPointer]);
firstPointer++;
} else if (arr1[firstPointer] === arr2[secondPointer]) {
newArray.push(arr1[firstPointer]);
newArray.push(arr2[secondPointer]);
firstPointer++;
secondPointer++;
} else if (arr1[firstPointer] > arr2[secondPointer]) {
newArray.push(arr2[secondPointer]);
secondPointer++;
} else if (arr1[firstPointer] === undefined) {
newArray.push(arr2[secondPointer]);
secondPointer++;
} else if (arr2[secondPointer] === undefined) {
newArray.push(arr1[firstPointer]);
firstPointer++;
}
}
}