-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckTheTwoArrayAreSameOrNot.js
42 lines (34 loc) · 1 KB
/
CheckTheTwoArrayAreSameOrNot.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
// Write a JavaScript program to determine if two arrays of integers of the same length are similar. The arrays will be similar if one array can be obtained from another array by swapping at most one pair of elements.
const prompt = require("prompt-sync")();
console.log("Enter the size of the Array-1 : ");
var size1 = parseInt(prompt());
console.log("Enter the size of the Array-2 : ");
var size2 = parseInt(prompt());
var arr1 = [];
var arr2 = [];
console.log("\n");
console.log("Enter the Array-1 Elements : ");
for (let i = 0; i < size1; i++) {
console.log(`[${i}] : `);
var elem = parseInt(prompt());
arr1.push(elem);
}
console.log("\n");
console.log("Enter the Array-2 Elements : ");
for (let i = 0; i < size2; i++) {
console.log(`[${i}] : `);
var elem = parseInt(prompt());
arr2.push(elem);
}
var flag = true;
for (let i = 0; i < size1; i++) {
if (arr1[i] !== arr2[i]) {
flag = false;
} else {
flag = true;
}
}
console.log("\n");
console.log(arr1);
console.log(arr2);
console.log(flag);