How to get index of first and last position of repeating elements in an array in scala
admin
#Scala, first and last index, #repeating elements, #array in Scala, #find index in Scala,
When working with arrays in Scala, one common task is to find the first and last occurrence of a repeating element. This article will guide you through an easy-to-understand approach for identifying the positions of repeating elements in an array, using a simple Scala program.
Problem: Let's assume you have an input array like this:
val arr = Array(3, 3, 4, 0, 4, 7, 7, 7)
val input = 7
Your task is to find the first and last positions of the element 7
in this array. This solution will be independent of the array size and can be applied to any repeating element in any array.
To find the index of the first and last occurrences of the input element in the array, we can use a loop to iterate through the array. During this loop, we track both the first and last index of the element whenever we encounter it.
Here’s a step-by-step breakdown of the Scala code that helps us achieve this:
val arr = Array(3, 3, 4, 0, 4, 7, 7, 7) // Input array
val input = 7 // Element to search
var count = 0 // To track how many times the element is found
var index = 0 // To store the first occurrence index
var pointer = 0 // Pointer for current index in the loop
for (ar <- arr) {
if (ar == input) {
// If the element matches the input
if (count == 0) {
// First occurrence
index = pointer
println(s"First index of input $input is: $index")
}
count += 1 // Increment count for each occurrence
}
pointer += 1 // Move to the next index
}
// Print the last occurrence index
println(s"Last index of element $input is: ${index + count - 1}")
When the program is run, the output will be:
First index of input 7 is: 5
Last index of element 7 is: 7
Array Setup: We define an array arr
and the element input
that we are searching for in the array.
Initial Variables:
count
: This variable counts how many times the element appears in the array.index
: This will store the index of the first occurrence of the element.pointer
: This is used to keep track of the current index as we iterate over the array.Loop Through the Array:
input
.count
is 0, we set the index
to the current pointer position, which is the first occurrence.count
to keep track of how many times the element has appeared.Last Index Calculation: After the loop completes, we calculate the last occurrence index by adding the count (minus one) to the first index, ensuring we have the correct last position.
This Scala approach efficiently finds the first and last index of repeating elements in an array. In the example above, we searched for the number 7
in the array, and the program correctly identified that its first occurrence was at index 5
and its last occurrence was at index 7
.
By using this approach, you can easily adapt the code to search for any element and calculate its positions in any given array. This method is simple, efficient, and works well with larger datasets as well.
If you have any queries or need further clarification, feel free to reach out to us via email. We hope this solution helps you in your Scala programming endeavors!