-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergingTwoArraysWithThirdArray.js
More file actions
62 lines (43 loc) · 1.11 KB
/
mergingTwoArraysWithThirdArray.js
File metadata and controls
62 lines (43 loc) · 1.11 KB
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
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<body>
<h2>Merging two arrays in a single array</h2>
<script>
function initialize (){
var arrayA = [2,10,18,20,23];
var arrayB = [4,9,19,25];
var arrayC = [];
//Call to the merge function
mergeArray(arrayA,arrayB,arrayC);
document.write(arrayC);
}
function mergeArray(arrayA,arrayB,arrayC){
var arrayAlength = arrayA.length;
var arrayBlength = arrayB.length;
//initialize with zero value as index starts from 0
var i,j,k;
i = j = k =0;
//Iterate over the two arrays A and B
while(i < arrayAlength && j < arrayBlength ){
//If array A as lesser element than array B then store that element in the array C and increase the indexes to give chances to next element
if(arrayA[i] < arrayB[j]){
arrayC[k++] = arrayA[i++];
}
//viceversa of above statement
else{
arrayC[k++] = arrayB[j++];
}
}
// if there are any elements are still left in the single array whether A or B then Iterate over them and store it into arrayC
for(;i<arrayAlength;i++){
arrayC[k++] = arrayA[i++];
}
for(;j<arrayBlength;j++){
arrayC[k++] = arrayB[j++];
}
}
// Call to Initizalize
initialize();
</script>
</body>
</html>