How to get index of first and last position of repeating elements in an array in scala

admin

7/2/2023
All Articles

  #Scala, first and last index, #repeating elements, #array in Scala, #find index in Scala,

How to get  index  of first  and last position of repeating elements in an array in scala

How to Get the Index of First and Last Position of Repeating Elements in an Array 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.

Approach to Solve the Problem

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.

Scala Code Example

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}")

Output:

When the program is run, the output will be:

First index of input 7 is: 5
Last index of element 7 is: 7

Explanation of the Code:

  1. Array Setup: We define an array arr and the element input that we are searching for in the array.

  2. 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.
  3. Loop Through the Array:

    • For each element in the array, we check if it matches the input.
    • If it matches and count is 0, we set the index to the current pointer position, which is the first occurrence.
    • We increment count to keep track of how many times the element has appeared.
    • The pointer is then incremented to move to the next element.
  4. 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.

Conclusion

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.

Final Thoughts

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!