如何在Go语言中创建地图并查找重复项

huangapple go评论81阅读模式
英文:

How to create a map and find duplicates in go

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习Go语言,无法弄清楚如何完成这个任务:

编写一个程序,在第一行从标准输入中读取连续数字的数量(范围为[1; 9]),然后将其加载到数组中。然后将指定数组值的重复实例数量打印到标准输出。

输入:
7
1 1 2 2 1 3 5
输出:
1: 3
2: 2
3: 1
5: 1

我已经完成了这个任务的一半,但是我无法弄清楚如何通过映射进行重复跟踪。

func main() {
    var numbers int
    fmt.Println("num: ")
    fmt.Scan(&numbers)
    var numArr int
    var i = 1
    arr := make([]int, 0)

    if numbers <= 9 {
        for ; i <= numbers; i++ {

            fmt.Println("numArr: ")
            fmt.Scan(&numArr)
            if numArr < 9 {
                arr = append(arr, numArr)
            } else {
                fmt.Println("sorry write the number up to 9")
                break
            }

        }

    } else {
        fmt.Println("Max Numbers = 9")
    }
    fmt.Println(arr)

    m := make(map[int]int)

    for key, v := range arr {
        
        
    }
}

请问你需要我翻译什么内容?

英文:

I'm new to go and can't figure out how to do this task:

> Write a program that, in the first line, reads from the standard input
> the number of consecutive digits in the range [1; 9], which will then
> be loaded into the array. Then prints the number of repeated instances
> of the specified array value to the standard output.

Input:
7
1 1 2 2 1 3 5
Output:
1: 3
2: 2
3: 1
5: 1

I have completed this task halfway, but I can't figure out how to do duplicate tracking via map

func main() {
	var numbers int
	fmt.Println(&quot;num: &quot;)
	fmt.Scan(&amp;numbers)
	var numArr int
	var i = 1
	arr := make([]int, 0)

	if numbers &lt;= 9 {
		for ; i &lt;= numbers; i++ {

			fmt.Println(&quot;numArr: &quot;)
			fmt.Scan(&amp;numArr)
			if numArr &lt; 9 {
				arr = append(arr, numArr)
			} else {
				fmt.Println(&quot;sorry write the number up to 9&quot;)
				break
			}

		}

	} else {
		fmt.Println(&quot;Max Numbers = 9&quot;)
	}
	fmt.Println(arr)

	m := make(map[int]int)

	for key, v := range arr {
		
		
	}

答案1

得分: 0

看起来你正在尝试用map[文件中的值]计数来填充地图。因此,每个接收到的不同数字都会有一个条目,并且地图中保存的值将是该值(键)出现的次数。填充这样的地图的一种简单方法是:

for _, v := range arr {
    m[v]++
}

Playground。请注意,map是无序的,所以在输出结果之前可能需要对其进行排序。

注意:你可以通过将正常路径与左边缘对齐来简化代码(并修复一个微妙的错误),即:

if numbers > 9 {
    fmt.Println("最大数字 = 9")
    return // 重要 - 否则我们仍然会尝试在“大”切片中查找重复项
}
// 处理只有9个或更少整数时应发生的事情
英文:

It looks like you are tying to populate the map with map[Value From File]Count. So there will be an entry for each distinct number received and the value held in the map will be the number of times the value (key) has been seen. A simple way to populate such a map is:

for _, v := range arr {
		m[v]++
}

Playground. Note that a map is unordered so you may need to sort the results before you output them.

Note: You can simplify you code (and fix a subtle bug) by aligning the happy path to the left edge i.e.

if numbers &gt; 9 {
   fmt.Println(&quot;Max Numbers = 9&quot;)
   return // Important - otherwise we will still try to find duplicates in the &#39;large&#39; slice
}
// Do stuff that should happen when we have 9 or fewer ints

huangapple
  • 本文由 发表于 2022年3月17日 08:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/71505793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定