-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom-array.ts
101 lines (92 loc) · 2.84 KB
/
custom-array.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
export default class CustomArray {
private length: number;
private data: Map<number, string | number>;
constructor() {
this.length = 0;
this.data = new Map();
}
/**
* The `push()` method adds element to the end of an array and returns the new length of the array.
* @param item Add string or number
* @returns length of an array
*/
public push(item: string | number): number {
this.data.set(this.length, item);
this.length++;
return this.length;
}
/**
* The `getLength()` returns length of an array
*/
public getLength(): number {
return this.length;
}
/**
* The `get()` method takes an integer value and returns the item at that index
* @param index integer values
* @returns the item at that index
*/
public get(index: number): string | number {
if (index < this.length && index >= 0) {
return this.data.get(index)!;
} else throw new Error("Array index out of bound");
}
/**
* The `pop()` method removes the last element from an array and returns that element.
*/
public pop() {
if (this.length <= 0) {
throw new Error("Array is empty cannot perform pop() action");
}
const item = this.data.get(this.length - 1);
this.data.delete(this.length - 1);
this.length--;
return item;
}
/**
* The `insert()` method adds the given value in the specified index
* @param index in which index the values need to be added
* @param value number or string value to be added
* @returns
*/
public insert(index: number, value: string | number): number {
if (index < 0 || index > this.length) {
throw new Error("Index is unavailable");
}
this.shiftRight(index);
this.data.set(index, value);
return this.length;
}
/**
* When an element in the array has been inserted;
* value in the that index of an array has to be shifted to right; till the end of an array
* @param index from which index the values to replaced
*/
private shiftRight(index: number) {
for (let i = this.length; i >= index; i--) {
this.data.set(i, this.data.get(i - 1) as number | string);
}
this.length++;
}
/**
* The `delete()` method deletes the item for the given index and return the length
* @param index deletes the item for the given index
*/
public delete(index: number): number {
this.data.delete(index);
this.shiftLeft(index);
return this.length;
}
/**
* When an element in the array has been deleted;
* values in the next index of an array has to be shifted to that place till the end of an array
* @param index from which index the values to replaced
*/
private shiftLeft(index: number) {
for (let i = index; i < this.length - 1; i++) {
this.data.set(i, this.data.get(i + 1) as number | string);
}
this.data.delete(this.length - 1);
this.length--;
}
}