Skip to content

Commit 1e77180

Browse files
committed
Array and String Problems
1 parent 406a9d0 commit 1e77180

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

DSA.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,33 @@ function DSA46() {
18471847

18481848
console.log(difference(arr1, arr2)); // [ 5, 6 ]
18491849
console.log(difference(arr2, arr1)); // [ 7, 8 ]
1850+
1851+
function difference2() {
1852+
const arr1 = ["a", "b", "c", "d", "e", "f"];
1853+
const arr2 = ["c", "e", "z"];
1854+
const result = [];
1855+
let i = 0;
1856+
let j = 0;
1857+
let k = 0;
1858+
1859+
while (i < arr1.length && j < arr2.length) {
1860+
if (arr1[i] === arr2[j]) {
1861+
i++;
1862+
j++;
1863+
} else if (arr1[i] < arr2[j]) {
1864+
result[k++] = arr1[i++];
1865+
} else {
1866+
j++;
1867+
}
1868+
}
1869+
1870+
while (i < arr1.length) {
1871+
result[k++] = arr1[i++];
1872+
}
1873+
1874+
console.log(result);
1875+
}
1876+
difference2();
18501877
}
18511878
// DSA46();
18521879

@@ -3267,3 +3294,35 @@ function DSA88() {
32673294
console.log(removeDuplicateswithIndex(str2));
32683295
}
32693296
// DSA88();
3297+
3298+
// 👉 89) Implement the chessBoard pattern
3299+
/*
3300+
W B W B W B W B
3301+
B W B W B W B w
3302+
W B W B W B W B
3303+
B W B W B W B w
3304+
W B W B W B W B
3305+
B W B W B W B w
3306+
W B W B W B W B
3307+
B W B W B W B w
3308+
*/
3309+
function DSA89() {
3310+
let chessBoard = [];
3311+
3312+
for (let i = 0; i < 8; i++) {
3313+
let temp = [];
3314+
for (let j = 0; j < 8; j++) {
3315+
if ((i + j) % 2 === 0) {
3316+
temp.push("W");
3317+
} else {
3318+
temp.push("B");
3319+
}
3320+
}
3321+
chessBoard.push(temp);
3322+
}
3323+
3324+
chessBoard.map((row) => {
3325+
console.log(...row);
3326+
});
3327+
}
3328+
// DSA89();

0 commit comments

Comments
 (0)