Skip to content

Commit 4fab3d8

Browse files
committed
update
1 parent 9cead49 commit 4fab3d8

File tree

13 files changed

+261
-32
lines changed

13 files changed

+261
-32
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// pattern 1
2+
3+
// 1
4+
// 1 2
5+
// 1 2 3
6+
// 1 2 3 4
7+
// 1 2 3 4 5
8+
9+
const n = 5;
10+
for (let i = 1; i <= n; i++) {
11+
let str = ""
12+
for (let j = 1; j <= i; j++) {
13+
str += j + " ";
14+
}
15+
console.log(str.trim())
16+
}
17+
18+
// A
19+
// A B
20+
// A B C
21+
// A B C D
22+
// A B C D E
23+
24+
for (let i = 1; i <= n; i++) {
25+
let str = ""
26+
for (let j = 1; j <= i; j++) {
27+
str += String.fromCharCode(64 + j) + " ";
28+
}
29+
console.log(str.trim())
30+
}
31+
32+
// *
33+
// * *
34+
// * * *
35+
// * * * *
36+
// * * * * *
37+
for (let i = 1; i <= n; i++) {
38+
let str = ""
39+
for (let j = 1; j <= i; j++) {
40+
str += "* ";
41+
}
42+
console.log(str.trim())
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// pattern 2
2+
// 5 4 3 2 1
3+
// 5 4 3 2
4+
// 5 4 3
5+
// 5 4
6+
// 5
7+
8+
const n = 5;
9+
for (let i = 1; i <= n; i++) {
10+
let str = " "
11+
for (let j = n; j >= i; j--) {
12+
str = str + j + " "
13+
}
14+
console.log(str.trim())
15+
}
16+
// .....................Star...... ... .. . . . . . . .. .
17+
18+
// * * * * *
19+
// * * * *
20+
// * * *
21+
// * *
22+
// *
23+
for (let i = 1; i <= n; i++) {
24+
let str = " "
25+
for (let j = n; j >= i; j--) {
26+
str += "* "
27+
}
28+
console.log(str.trim())
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
// pattern 3
3+
4+
// 1 2 3 4 5
5+
// 2 3 4 5
6+
// 3 4 5
7+
// 4 5
8+
// 5
9+
10+
const n = 5;
11+
for (let i = 1; i <= n; i++) {
12+
let str = "";
13+
for (let j = i; j <= n; j++) {
14+
str += j + " ";
15+
}
16+
console.log(str.trim());
17+
}
18+
19+
// .....................Star...... ... .. . . . . . . .. .
20+
21+
// * * * * *
22+
// * * * *
23+
// * * *
24+
// * *
25+
// *
26+
for (let i = 1; i <= n; i++) {
27+
let str = "";
28+
for (let j = i; j <= n; j++) {
29+
str += "* ";
30+
}
31+
console.log(str.trim());
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// pattern 4
2+
3+
// 1
4+
// 12
5+
// 123
6+
// 1234
7+
8+
const n = 5;
9+
for (let i = 1; i <= n; i++) {
10+
let str = '';
11+
12+
for (let j = 1; j <= (n - i); j++) {
13+
str += " ";
14+
}
15+
16+
for (let k = 1; k <= i; k++) {
17+
str += k;
18+
}
19+
20+
console.log(str);
21+
}
22+
23+
// *
24+
// **
25+
// ***
26+
// ****
27+
// *****
28+
29+
for (let i = 1; i <= n; i++) {
30+
let str = '';
31+
32+
for (let j = 1; j <= (n - i); j++) {
33+
str += " ";
34+
}
35+
36+
for (let k = 1; k <= i; k++) {
37+
str += "*";
38+
}
39+
40+
console.log(str);
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// pattern with start
2+
// *
3+
// ***
4+
// *****
5+
// *******
6+
// *********
7+
let n = 5;
8+
for (let i = 1; i <= n; i++) {
9+
let str = "";
10+
for (let j = 1; j <= n + 4; j++) {
11+
if (j >= (6 - i) && j <= (4 + i)) {
12+
str += "*";
13+
} else {
14+
str += " ";
15+
}
16+
}
17+
console.log(str);
18+
}
19+
20+
21+
// pattern with number
22+
23+
// 1
24+
// 123
25+
// 12345
26+
// 1234567
27+
// 123456789
28+
29+
for (let i = 1; i <= n; i++) {
30+
let str = "";
31+
for (let j = 1; j <= n + 4; j++) {
32+
if (j >= (6 - i) && j <= (4 + i)) {
33+
str += (j - (n - i));
34+
} else {
35+
str += " ";
36+
}
37+
}
38+
console.log(str);
39+
}
40+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// pattern
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"*";

coding_series/22.union_arr.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
// 22. Write a JavaScript program to compute the union of two arrays.
2-
// Sample Data :
3-
function unionArr(arr1, arr2) {
4-
let unionArr = []
5-
for (let i = 0; i < arr1.length; i++) {
6-
for (let j = 0; j < arr2.length; j++) {
7-
if (arr1[i] === arr2[j]) {
8-
unionArr.push(arr1[i])
9-
}
10-
}
11-
}
12-
return (unionArr)
2+
function union(arr1, arr2) {
3+
const newArr = ([...arr1, ...arr2])
4+
const uniqueArr = ([...new Set(newArr)])
5+
return (uniqueArr)
6+
}
7+
8+
console.log("Union of two Array is : ", union([1, 2, 3], [100, 2, 1, 10]));
139

10+
// another method
11+
12+
function union1(arr1, arr2) {
13+
const result = [];
14+
const map = {}
15+
arr1.forEach(element => {
16+
if (!map[element]) {
17+
map[element] = true;
18+
result.push(element);
19+
}
20+
});
21+
arr2.map((element) => {
22+
if (!map[element]) {
23+
map[element] = true;
24+
result.push(element);
25+
}
26+
})
27+
return result;
1428
}
1529

16-
const array1 = [1, 2, 3];
17-
const array2 = [100, 2, 1, 10];
18-
console.log(unionArr(array1, array2))
30+
console.log("Union of two arrays is: ", union1([1, 2, 3], [100, 2, 1, 10]));

coding_series/23_difference_Arr.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
// 23. Write a JavaScript function to find the difference between two arrays.
2-
3-
function differenceArr(arr1, arr2) {
4-
5-
}
6-
const array1 = [1, 2, 3];
7-
const array2 = [100, 2, 1, 10];

coding_series/single_ele.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// Using inbuilt Methods
3+
4+
const arr = [1, 1, 2, 3, 3, 4, 4, 6, 7, 7, 2, 9];
5+
6+
const uniqueElement = arr.filter((item) => arr.indexOf(item) === arr.lastIndexOf(item))
7+
8+
console.log(`Unique element: ${uniqueElement}`);
9+
10+
const findUniqueElements = (arr) => {
11+
const elementCount = {};
12+
13+
// Step 1: Count the occurrences of each element in the array
14+
for (let i = 0; i < arr.length; i++) {
15+
if (elementCount[arr[i]]) {
16+
elementCount[arr[i]]++;
17+
} else {
18+
elementCount[arr[i]] = 1;
19+
}
20+
}
21+
22+
// Step 2: Collect all elements that appear only once
23+
const uniqueElements = [];
24+
for (let i = 0; i < arr.length; i++) {
25+
if (elementCount[arr[i]] === 1) {
26+
uniqueElements.push(arr[i]);
27+
}
28+
}
29+
30+
return uniqueElements; // Return an array of all unique elements
31+
};
32+
33+
const uniqueElements = findUniqueElements(arr);
34+
console.log(`Unique elements: ${uniqueElements}`);
35+
36+

0 commit comments

Comments
 (0)