DEV Community

NDERAKORE5000
NDERAKORE5000

Posted on

10 Essential Array Exercises for Any Programming Language

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 


Enter fullscreen mode Exit fullscreen mode

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 


Enter fullscreen mode Exit fullscreen mode

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 


Enter fullscreen mode Exit fullscreen mode

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] 


Enter fullscreen mode Exit fullscreen mode

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 


Enter fullscreen mode Exit fullscreen mode

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] 


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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]


Enter fullscreen mode Exit fullscreen mode

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] 


Enter fullscreen mode Exit fullscreen mode

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 


Enter fullscreen mode Exit fullscreen mode

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay