type or paste code here
func HighestRank(a []int) int{
count :=1
var tempCount int
var popular int =a[0]
var temp int = 0
for i := 0; i < len(a); i++{
temp = a[i]// holding the current element at each iteration
tempCount = 0//this is maintain for counting the frequency of each elements so must be start from zero
for j := 1; j < len(a); j++{
if (temp == a[j]){//counting the frequencies
tempCount = (tempCount +1) }
}
if (tempCount > count){// current element count is greater than the previous holding value of count
popular = temp//then have current element into popular
count = tempCount//make highest frequencies as count upto
}
}
return popular
}
func main() {
arr := []int{30 ,30, 10, 10, 20, 20}//output 30
ret := HighestRank(arr)
fmt.Println(ret)
}
The problem with your code is lackof checking the frequency of temp and popular are the same.
I did some modifications (Also return the frequency of the number)
func HighestRank(a []int) (int, int) {
popular, count := a[0], 1
for i := 0; i < len(a); i++ {
temp, tempCount := a[i], 1
for j := i + 1; j < len(a); j++{
if (temp == a[j]){
tempCount++
}
}
if (tempCount > count || (tempCount == count && temp > popular)){
popular = temp
count = tempCount
}
}
return popular, count
}
thank you for the response