0% found this document useful (0 votes)
6 views

Array Function

Uploaded by

nand sangani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Array Function

Uploaded by

nand sangani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

array function

- All questions needs to be solved using function and return value

1. Write a function to find the maximum element in an array.


const array1 = [3, 7, 1, 5, 9, 2];
console.log(findMaxElement(array1)); // Output: 9

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]

3. Implement a function to calculate the sum of all elements in an array.


const array3 = [2, 8, 3, 6, 1];
console.log(calculateArraySum(array3)); // Output: 20

4. Write a function to reverse the order of elements in an array.


const array4 = [1, 2, 3, 4, 5];
console.log(reverseArray(array4)); // Output: [5, 4, 3, 2, 1]

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

6. Implement a function to remove duplicates from an array while preserving the


order.
const array6 = [3, 5, 2, 3, 7, 5, 8];
console.log(removeDuplicates(array6)); // Output: [3, 5, 2, 7, 8]

7. Write a function to square each even number in an array.


const array7 = [2, 5, 3, 8, 10];
console.log(squareEvenNumbers(array7)); // Output: [4, 64, 100]

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]

9. Implement a function to rotate the elements of an array to the right by a given


number of positions.
const array9 = [1, 2, 3, 4, 5];
const rotatePositions = 2;
console.log(rotateArray(array9, rotatePositions)); // Output: [4, 5, 1, 2, 3]

10. Write a function to check if an array of characters forms a palindrome.


const array10 = ['a', 'b', 'c', 'b', 'a'];
console.log(isPalindrome(array10)); // Output: true

11. Implement a function to divide an array into chunks of a specified size.


const array11 = [1, 2, 3, 4, 5, 6, 7];
const chunkSize = 3;
console.log(chunkArray(array11, chunkSize)); // Output: [[1, 2, 3], [4, 5, 6], [7]]
12. Write a function that returns an array containing elements common to multiple
arrays.
const array12_1 = [3, 5, 2, 7];
const array12_2 = [4, 2, 8, 3];
console.log(findIntersection(array12_1, array12_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]

14. Implement a function that checks if an array contains consecutive elements.


const array14 = [4, 2, 7, 5, 3];
console.log(hasConsecutiveElements(array14)); // Output: true

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

You might also like