10 Essential Array Exercises for Any Programming Language
1. Sum of Array Elements Write a function that takes an array of numbers and returns the sum of its elements.
Example:
Input: [1, 2, 3, 4, 5]
Output: 15
2. Find the Maximum Value Write a function that finds and returns the maximum value in an array of numbers.
Example:
Input: [7, 3, 5, 2, 8]
Output: 8
3. Find the Minimum Value Write a function that finds and returns the minimum value in an array of numbers.
Example:
Input: [7, 3, 5, 2, 8]
Output: 2
4. Reverse an Array Write a function that reverses the order of elements in an array. Example:
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
5. Check for Duplicates Write a function that checks if there are any duplicate elements in an array and returns true
if there are duplicates, or false
otherwise.
Example:
Input: [1, 2, 3, 4, 5, 2]
Output: true
6. Remove Duplicates Write a function that removes all duplicate elements from an array and returns a new array with only unique elements. Example:
Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 2, 3, 4, 5]
7. Find the Average Write a function that calculates the average value of an array of numbers. Example:
Input: [1, 2, 3, 4, 5]
Output: 3.0
8. Rotate Array Write a function that rotates the elements of an array to the right by a given number of positions.
Example:
Input: [1, 2, 3, 4, 5],
positions: 2
Output: [4, 5, 1, 2, 3]
9. Find Common Elements Write a function that takes two arrays and returns an array containing only the elements that are common to both arrays. Example:
Input: [1, 2, 3, 4], [3, 4, 5, 6]
Output: [3, 4]
10. Array Product Write a function that computes the product of all elements in an array of numbers.
Example:
Input: [1, 2, 3, 4]
Output: 24
These exercises are designed to help you practice fundamental array operations and can be implemented in any programming language. They provide a good starting point for developing your skills in array manipulation and algorithm design.
Top comments (0)