w3resource

Scala Programming Array - Exercises, Practice, Solution

Scala Programming Array [40 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Scala program to sum values of an given array. Go to the editor
Click me to see the sample solution

2. Write a Scala program to check if a given number is present in fast or last position of a given array of length 1 or more. Go to the editor
Click me to see the sample solution

3. Write a Scala program to calculate the average value of an array of element. Go to the editor
Click me to see the sample solution

4. Write a Scala program to check if the value of the fast or last element of a given array ( length 1 or more) are same or not. Go to the editor
Click me to see the sample solution

5. Write a Scala program to find the index of an element in a given Array. Go to the editor
Click me to see the sample solution

6. Write a Scala program to check whether the value of the fast or last element of two given array ( length 1 or more) of integers are same or not. Go to the editor
Click me to see the sample solution

7. Write a Scala program to remove a specific element from an given array. Go to the editor
Note: The size of an Array can't be changed, so we can't directly delete elements from an array but replace them with "" / null etc.
Click me to see the sample solution

8. Write a Scala program to rotate one element left of an given array (length 1 or more) of integers. Go to the editor
Click me to see the sample solution

9. Write a Scala program to find the maximum and minimum value of an array of integers. Go to the editor
Click me to see the sample solution

10. Write a Scala program to calculate the sum of the last 3 elements of an array of integers. If the array length is less than 3 then return the sum of the array. Return 0 if the array is empty. Go to the editor
Click me to see the sample solution

11. Write a Scala program to create a new array taking the middle element from three arrays of length 5. Go to the editor
Click me to see the sample solution

12. Write a Scala program to reverse an array of integer values. Go to the editor
Click me to see the sample solution

13. Write a Scala program to check two numbers, 4 or 7 present in a given array of integers. Go to the editor
Click me to see the sample solution

14. Write a Scala program to find the maximum value from first, middle and last values of a given array of integers. Array length should be 1 and more and odd. Go to the editor
Click me to see the sample solution

15. Write a Scala program to find the common elements between two arrays of integers. Go to the editor
Click me to see the sample solution

16. Write a Scala program to find the common elements between two arrays of strings. Go to the editor
Click me to see the sample solution

17. Write a Scala program to remove duplicate elements from an array of strings. Go to the editor
Click me to see the sample solution

18. Write a Scala program to remove duplicate elements from an array of integers. Go to the editor
Click me to see the sample solution

19. Write a Scala program to find the second largest element from a given array of integers. Go to the editor
Click me to see the sample solution

20. Write a Scala program to find the second smallest element from a given array of integers. Go to the editor
Click me to see the sample solution

21. Write a Scala program to test the equality of two arrays. Go to the editor
Click me to see the sample solution

22. Write a Scala program to find a missing number in an array of integers. Go to the editor
Click me to see the sample solution

23. Write a Scala program to find the number of even and odd integers in a given array of integers. Go to the editor
Click me to see the sample solution

24. Write a Scala program to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above. Go to the editor
Click me to see the sample solution

25. Write a Scala program to compute the average value of an array element except the largest and smallest values. Go to the editor
Click me to see the sample solution

26. Write a Scala program to remove the duplicate elements of a given sorted array and return the new length of the array. Go to the editor
Click me to see the sample solution

27. Write a Scala program to find smallest and second smallest elements of a given array. Go to the editor
Click me to see the sample solution

28. Write a Scala program to segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s. Go to the editor
Click me to see the sample solution

29. Write a Scala program to find the two elements from a given array of positive and negative numbers such that their sum is closest to zero. Go to the editor
Click me to see the sample solution

30. Write a Scala program to find all combination of four elements of a given array whose sum is equal to a given value. Go to the editor
Click me to see the sample solution

31. Write a Scala program to count the number of possible triangles from a given unsorted array of positive integers. Go to the editor
Note: The triangle inequality states that the sum of the lengths of any two sides of a triangle must be greater than or equal to the length of the third side.
Click me to see the sample solution

32. Write a Java program to arrange the elements of a given array of integers where all positive integers appear before all the negative integers. Go to the editor
Click me to see the sample solution

33. Write a Scala program to separate even and odd numbers of a given array of integers. Put all even numbers first, and then odd numbers. Go to the editor
Click me to see the sample solution

34. Write a Scala program to replace every element with the next greatest element (from right side) in a given array of integers. There is no element next to the last element, therefore replace it with -1. Go to the editor
Click me to see the sample solution

35. Write a Scala program to find all pairs of elements in an array whose sum is equal to a specified number. Go to the editor
Click me to see the sample solution

36. Write a Scala program to find maximum product of two integers in a given array of integers. Go to the editor
Sample Input:
nums = { 2, 3, 5, 7, -7, 5, 8, -5 }
Sample Output:
Pair is (7, 8), Maximum Product: 56
Click me to see the sample solution

37. Write a Scala program to rearrange a given array of unique elements such that every second element of the array is greater than its left and right elements. Go to the editor
Salple Input:
nums= { 1, 2, 4, 9, 5, 3, 8, 7, 10, 12, 14 }
Sample Output:
Array with every second element is greater than its left and right elements:
[1, 4, 2, 9, 3, 8, 5, 10, 7, 14, 12]
Click me to see the sample solution

38. Write a Scala program to find maximum difference between two elements in a given array of integers such that smaller element appears before larger element. Go to the editor
Sample Input:
nums = { 2, 3, 1, 7, 9, 5, 11, 3, 5 }
Sample Output:
The maximum difference between two elements of the said array elements
10
Click me to see the sample solution

39. Write a Scala program to find contiguous subarray within a given array of integers which has the largest sum. Go to the editor
Sample Input:
int[] A = {1, 2, -3, -4, 0, 6, 7, 8, 9}
Sample Output:
The largest sum of contiguous sub-array: 30
Click me to see the sample solution

40. Write a Scala program to find minimum subarray sum of specified size in a given array of integers. Go to the editor

Sample Input:
nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10}
Sample Output:
Sub-array size: 4
Sub-array from 0 to 3 and sum is: 10
Click me to see the sample solution

More...

Scala Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz