Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang Program to Find the Position of the Rightmost Number

Help me, please.

Please make Golang program to find the position of the rightmost number. For example:
There are 10 arrays one dimention
Number: 1 4 7 8 9 2 3 5 3 2
Position: 0 1 2 3 4 5 6 7 8 9
Input the number you want to find the position: 3
Output: The rightmost position of 3 is at index 8

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This can be done with a for loop

numbers := // slice of 1 4 7 8 9 2 3 5 3 2
numberToFind := 3

found := false
var position int

for index, number := range numbers {
   if number == numberToFind {
      found = true
      position = index
   }
}

if found {
   fmt.Println(fmt.Sprintf("found rightmost index of %v at %v", numberToFind, position))
} else {
   fmt.Println("number is not in the slice")
}

Running in Go Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading