Saturday, September 30, 2023
HomeSoftware EngineeringHighest Rank Quantity in an Array in Golang

Highest Rank Quantity in an Array in Golang


The problem

Full the strategy which returns the quantity which is most frequent within the given enter array. If there’s a tie for probably the most frequent quantity, return the most important quantity amongst them.

Word: no empty arrays will likely be given.

Examples

[12, 10, 8, 12, 7, 6, 4, 10, 12]              -->  12
[12, 10, 8, 12, 7, 6, 4, 10, 12, 10]          -->  12
[12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10]  -->   3

The answer in Golang

Possibility 1:

bundle resolution
func HighestRank(nums []int) int {
  mii, maxK, maxV := map[int]int{}, 0, 0
  for _, v := vary nums {
    mii[v]++
    if mii[v] > maxV || (mii[v] == maxV && v > maxK) {
      maxK = v
      maxV = mii[v]
    }
  }
  return maxK
}

Possibility 2:

bundle resolution
func HighestRank(nums []int) int {
  fs := make(map[int]int)
  for _, n := vary nums {
    fs[n]++
  }
  maxf, maxfn := 0, 0
  for n, f := vary fs {
    if f >= maxf && (f > maxf || n > maxfn) {
      maxf, maxfn = f, n
    }
  }
  return maxfn
}

Possibility 3:

bundle resolution
func HighestRank(nums []int) int {
  occurrences := make(map[int]int)
  var max, val int
  for _, worth := vary nums {
    occurrences[value]++
  }
  max, val = occurrences[nums[0]], nums[0]
  for worth, occasions := vary occurrences {
    if occasions > max {
      val = worth
      max = occasions
    } else if occasions == max {
      if worth > val {
        val = worth
      }
    }
  }
  return val
}

Take a look at circumstances to validate our resolution

bundle solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Exams", func() {
    Describe("Pattern assessments", func() {
        It("Pattern check 1: 12, 10, 8, 12, 7, 6, 4, 10, 12", func() {
            Anticipate(HighestRank([]int{12, 10, 8, 12, 7, 6, 4, 10, 12 })).To(Equal(12))
        })
    })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments