Array Function
Array Function
2. Create a function that filters out the odd numbers from an array.
const array2 = [1, 4, 7, 2, 9, 10];
console.log(filterOddNumbers(array2)); // Output: [4, 2, 10]
5. Create a function that searches for a specific element in an array and returns
its index.
const array5 = [8, 12, 6, 4, 10];
const targetElement = 6;
console.log(searchElement(array5, targetElement)); // Output: 2
8. Create a function that finds the common elements between two arrays.
const array8_1 = [3, 7, 2, 1];
const array8_2 = [4, 2, 8, 3];
console.log(findCommonElements(array8_1, array8_2)); // Output: [3, 2]
13. Create a function to flatten a nested array, maintaining the order of elements.
const array13 = [1, [2, 3], [4, [5, 6]]];
console.log(flattenArray(array13)); // Output: [1, 2, 3, 4, 5, 6]
15. Write a function to find the minimum absolute difference between any two
elements in an array.
const array15 = [4, 8, 2, 6, 10];
console.log(minAbsoluteDifference(array15)); // Output: 2
16. Create a function that moves all zeros in an array to the end while maintaining
the order of other elements.
const array16 = [0, 2, 0, 4, 6, 8, 0];
console.log(moveZerosToEnd(array16)); // Output: [2, 4, 6, 8, 0, 0, 0]
17. Write a function to check if one array is a rotated version of another array.
const array17_1 = [1, 2, 3, 4, 5];
const array17_2 = [4, 5, 1, 2, 3];
console.log(isRotatedArray(array17_1, array17_2)); // Output: true
18. Implement a function that finds two numbers in an array whose sum equals a
given target.
const array18 = [2, 7, 4, 11, 15];
const targetSum = 9;
console.log(findTwoNumbersSum(array18, targetSum));
// Output: [2, 7]
19. Create a function to compute the product of an array except for the element at
each index.
const array19 = [1, 2, 3, 4];
console.log(productExceptSelf(array19)); // Output: [24, 12, 8, 6]
20. Write a function to count the occurrences of each element in an array and
return the counts.
const array20 = [2, 5, 2, 8, 2, 5, 3];
console.log(countElementOccurrences(array20));
// Output: {2: 3, 5: 2, 8: 1, 3: 1}Thread - 0
Thread - 1
Thread - 2
Thread - 2
Thread - 0
Thread - 1
Thread - 0
Thread - 1
Thread - 2
Thread - 1
Thread - 0
Thread - 2
Thread - 2
Thread - 0
Thread - 1
Thread - 0
Thread - 1
Thread - 2
Thread - 0
Thread - 1
Thread - 2