-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-Day.js
29 lines (24 loc) · 961 Bytes
/
10-Day.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
// Hint: You may need to use nested loops and conditional statements to solve this problem.
function findCommonElements(arr1, arr2) {
// Create an empty array to hold the common elements
let commonElements = [];
// Loop through each element in arr1
for (let i = 0; i < arr1.length; i++) {
// Check if the current element is in arr2
if (arr2.includes(arr1[i])) {
// If the element is in arr2 and not already in commonElements array, add it
if (!commonElements.includes(arr1[i])) {
commonElements.push(arr1[i]);
}
}
}
// Sort the commonElements array in ascending order
commonElements.sort((a, b) => a - b);
// Return the commonElements array
return commonElements;
}
// Example usage
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [3, 4, 5, 6, 7];
const commonElements = findCommonElements(arr1, arr2);
console.log(commonElements); // Outputs: [3, 4, 5]