-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy patharray.js
55 lines (40 loc) · 1.21 KB
/
array.js
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
// Complexity: O(n)
function traverse(arr) {
for (let counter = 0; counter < arr.length; counter++) {
console.log(arr[counter]);
}
}
// Complexity: O(n)
function insertAtPosition(items, position, item) {
// Built-in option available in javascript
// items.splice(position, 0, item)
// return items;
// Push all the items to the right until position
for (let counter = items.length; counter > position; counter--) {
items[counter] = items[counter - 1];
}
items[position] = item;
return items;
}
// Complexity: O(n)
function deleteFromPosition(items, position) {
// Built-in option available in javascript
// items.splice(position, 1)
// return items;
for (let counter = position; counter < items.length; counter++) {
items[counter] = items[counter + 1];
}
return items;
}
const fruits = ["apple", "mango", "peach", "orange", "banana"];
// Accessing at index: O(1)
// console.log(fruits[3]);
// Traversing an array: O(n)
// traverse(fruits);
// Inserting at a position: O(n)
// traverse(insertAtPosition(fruits, 2, "grapes"));
// Deleting from position: O(n)
// traverse(deleteFromPosition(fruits, 2));
// Updating at position: O(1)
// fruits[0] = "pineapple";
// console.log(fruits);