英文:
GoLang Print the Number of Occurances of the Values in an Array
问题
以下是我翻译好的代码部分:
package main
import "fmt"
func main() {
// 初始化一个数组
inputArray := []int{10, 20, 30, 56, 67, 90, 10, 20}
printUniqueValue(inputArray)
}
func printUniqueValue(arr []int) {
// 创建一个用于存储每个元素值的字典
dict := make(map[int]int)
for _, num := range arr {
dict[num]++
}
fmt.Println(dict)
}
但是我无法按照我想要的方式构建字典,例如 dict[10]
应该有值 2。
预期输出示例:
dict[10] = 2
dict[20] = 2
dict[30] = 1
我得到的错误信息是:panic: assignment to entry in nil map
。
英文:
The following is my code where I need to Print the number of occurances of each values in an array.
package main
import "fmt"
func main(){
//Initialize an array
inputArray := []int{10,20,30,56,67,90,10,20}
printUniqueValue(inputArray)
}
func printUniqueValue( arr []int){
//Create a dictionary of values for each element
var dict map[int]int
count := 0
for _ , num := range arr {
dict[num] = count+1
}
fmt.Println(dict)
}
But I couldn't construct the dictionary as I wish like , dict[10] should have value 2.
Sample Expected Output :
dict[10] = 2
dict[20] = 2
dict[30] = 1
Error I got: panic: assignment to entry in nil map
答案1
得分: 15
package main
import "fmt"
func main() {
//初始化一个数组
inputArray := []int{10, 20, 30, 56, 67, 90, 10, 20}
printUniqueValue(inputArray)
}
func printUniqueValue(arr []int) {
//为每个元素创建一个值的字典
dict := make(map[int]int)
for _, num := range arr {
dict[num] = dict[num] + 1
}
fmt.Println(dict)
}
这段代码输出 map[67:1 90:1 10:2 20:2 30:1 56:1]
。
英文:
package main
import "fmt"
func main(){
//Initialize an array
inputArray := []int{10,20,30,56,67,90,10,20}
printUniqueValue(inputArray)
}
func printUniqueValue( arr []int){
//Create a dictionary of values for each element
dict:= make(map[int]int)
for _ , num := range arr {
dict[num] = dict[num]+1
}
fmt.Println(dict)
}
This prints map[67:1 90:1 10:2 20:2 30:1 56:1]
答案2
得分: 5
你需要使用非空的映射来初始化dict
变量;一种方法是使用var dict = make(map[int]int)
。
一旦你修复了这个问题,你还需要处理dict[num] = count+1
中的逻辑错误,其中该值的计数被设置为1(count始终为0),而不是比先前值多一个。
英文:
You need to initialize dict
with a non-nil map; one way would be with var dict = make(map[int]int)
.
Once you fix that, you will also need to deal with the logic error in dict[num] = count+1
, where the count of that value is set to 1 (count is always 0) instead of one more than the previous value.
答案3
得分: 1
你有几个问题。
- 你声明了
dict
作为一个映射值,但它没有被赋任何值,所以是空的。 - 你没有按照预期更新计数。
要修复映射的问题,使用make
函数将空映射赋给dict
变量。如上所述。
你期望的结果是:
map[10:2 20:2 30:1 56:1 67:1 90:1]
但即使映射被正确初始化,你也会得到:
map[10:1 20:1 30:1 56:1 67:1 90:1]
不要在映射之外使用计数器。使用现有的值。
根据Go教程,当使用不存在的键查询映射时,映射会返回零值。这对于这个任务来说是一个很好的特性,参考下面的代码:
package main
import "fmt"
func main(){
inputArray := []int{10,20,30,56,67,90,10,20}
printUniqueValue(inputArray)
}
func printUniqueValue(arr []int) {
dict := make(map[int]int)
for _ , num := range arr {
// 如果尚未初始化,dict[num]将返回0
dict[num] = dict[num] + 1
}
fmt.Println(dict)
}
英文:
You have a couple of problems here.
- You are declaring
dict
as a map value, but it is not assigned any value and so is nil - You are not updating the count as you are expecting
To fix the issue with the map, use the make function to assign an empty map to the dict
variable. As explained above.
You are expecting:
map[10:2 20:2 30:1 56:1 67:1 90:1]
But even if the map was initialised correctly, you would get:
map[10:1 20:1 30:1 56:1 67:1 90:1]
Don't use a counter outside of the map itself. Use the existing value.
According to the Go tour maps return a zero value when queried with a non-existing key. This is a nice feature for this task, see the below code
package main
import "fmt"
func main(){
inputArray := []int{10,20,30,56,67,90,10,20}
printUniqueValue(inputArray)
}
func printUniqueValue(arr []int) {
dict := make(map[int]int)
for _ , num := range arr {
// dict[num] will return 0 if it hasn't yet been initialised
dict[num] = dict[num] + 1
}
fmt.Println(dict)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论