题解 | #牛的体重统计#

牛的体重统计

https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf

package main

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param weightsA int整型一维数组 
 * @param weightsB int整型一维数组 
 * @return int整型
*/
func findMode( weightsA []int ,  weightsB []int ) int {
    // write code here
    countmap :=make(map[int]int)
    for _,val :=range weightsA{
        if _,ok :=countmap[val];!ok{
            countmap[val]=1
        }else{
            countmap[val]++
        }
    }
    for _,val :=range weightsB{
        if _,ok :=countmap[val];!ok{
            countmap[val]=1
        }else{
            countmap[val]++
        }
    }
    maxcount :=0
    maxweight :=0
    for weight,count :=range countmap{
        if count>=maxcount&&weight>maxweight{
            maxcount=count
            maxweight=weight
        }
    }
    return maxweight
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务