Skip to content

Commit a162491

Browse files
feat: add mastering arrays
1 parent c6baf80 commit a162491

File tree

19 files changed

+129
-853
lines changed

19 files changed

+129
-853
lines changed

Topic5/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Arrays | DSA</title>
7+
</head>
8+
<body>
9+
<script src="./index.js"></script>
10+
</body>
11+
</html>

Topic5/index.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
let arr = [];
2+
arr.push(100);
3+
arr.push(10);
4+
arr.push("ali");
5+
arr.push(false);
6+
7+
console.log(arr[0]);
8+
9+
console.log(arr);
10+
arr.pop();
11+
console.log(arr);
12+
13+
let arr1 = new Array(3);
14+
arr1[0] = 100;
15+
arr1[1] = 90;
16+
arr1[2] = 80;
17+
arr1[3] = 70;
18+
console.log(arr1);
19+
20+
let arr2 = new Array(5);
21+
for (let i = 0; i < arr2.length; i++) {
22+
arr2[i] = Number(prompt("Enter a value"));
23+
}
24+
console.log(arr2);
25+
26+
// sum of array
27+
let arr3 = [10, 20, 30, 40, 50];
28+
let sum = 0;
29+
for (let i = 0; i < arr3.length; i++) {
30+
sum += arr3[i];
31+
}
32+
console.log(sum);
33+
34+
// max
35+
let arr4 = [10, 2, 78, 100, 4];
36+
let max = arr4[0];
37+
for (let i = 0; i < arr4.length; i++) {
38+
if (max < arr4[i]) {
39+
max = arr4[i];
40+
}
41+
}
42+
console.log(max);
43+
44+
// Second max
45+
let arr5 = [10, 30, 56, 43, 29, 64, 49, 60];
46+
let max1 = Math.max(arr5[0], arr5[1]);
47+
let sMax = Math.min(arr5[0], arr5[1]);
48+
for (let i = 2; i < arr5.length; i++) {
49+
if (arr5[i] > max) {
50+
sMax = max;
51+
mar = arr5[i];
52+
} else if (arr5[i] > sMax && max != arr5[i]) {
53+
sMax = arr5[i];
54+
}
55+
}
56+
console.log(sMax);
57+
58+
// Reverse Array with extra space
59+
let arr6 = [10, 20, 30, 40, 50];
60+
let temp = new Array(arr6.length);
61+
62+
let j = 0;
63+
for (let i = arr6.length - 1; i >= 0; i--) {
64+
temp[j] = arr6[i];
65+
j++;
66+
}
67+
console.log(temp);
68+
69+
// my logic with push
70+
let arr7 = [10, 20, 30, 40, 50];
71+
let newArr7 = [];
72+
73+
for (let i = arr.length - 1; i >= 0; i--) {
74+
mainArr7.push(arr7[i]);
75+
}
76+
console.log(mainArr7);
77+
78+
// Reverse Array with no extra space & no extra array
79+
let arr8 = [10, 20, 30, 40, 50];
80+
let i = 0;
81+
let k = arr8.length - 1;
82+
83+
while (i !== j) {
84+
let temp = arr8[i];
85+
arr8[i] = arr8[j];
86+
arr8[j] = temp;
87+
i++;
88+
j--;
89+
}
90+
console.log(arr8);
91+
92+
// 0s on the left and 1s on right -> two pointer theory
93+
let arr9 = [0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0];
94+
let b = 0;
95+
for (let i = 0; i < arr9.length; i++) {
96+
if (arr9[i] === 0) {
97+
let temp = arr9[b];
98+
arr9[b] = arr9[i];
99+
arr9[i] = temp;
100+
b++;
101+
}
102+
}
103+
console.log(arr9);
104+
105+
// 0s on the left and 1s on right using while loop -> two pointer theory
106+
let arr10 = [0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0];
107+
let e = 0;
108+
let f = 0;
109+
while (e < arr10.length) {
110+
if (arr10[e] === 0) {
111+
let temp = arr10[f];
112+
arr10[f] = arr10[e];
113+
arr10[e] = temp;
114+
f++;
115+
}
116+
e++;
117+
}
118+
console.log(arr10);

node_modules/.package-lock.json

Lines changed: 0 additions & 37 deletions
This file was deleted.

node_modules/ansi-regex/index.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

node_modules/ansi-regex/license

Lines changed: 0 additions & 9 deletions
This file was deleted.

node_modules/ansi-regex/package.json

Lines changed: 0 additions & 53 deletions
This file was deleted.

node_modules/ansi-regex/readme.md

Lines changed: 0 additions & 87 deletions
This file was deleted.

node_modules/prompt-sync/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)