golang array—–find the missing integer method in the array


question:Find the missing integers in an unsorted array of n-1 integers, where the elements are all different integers from 1 to n

method one:

Ideas:is the sum of the original array minus the sum of the array after the lost element, and the lost element is obtained

code show as below:

package main
import (
func getMissingElement(arr []int) int {
	var sumA, sumB int
	if arr == nil || len(arr) <= 0 {
		errors.New(&quot;empty array&quot;)
	for k, v := range arr {
		sumA += v
		sumB += k
	sumB = sumB + len(arr)*2 + 1
	return sumB - sumA
func main() {
	var arr []int
	arr = []int{1, 3, 2, 6, 5, 7, 8}

Result: 4

Supplement: Common pits for golang rookies—-golang slices and arrays

Difference between slice and array:

Once an array (array:=[len]int{}) is declared and defined, it will have a fixed length (len), a fixed capacity (cap), and the length of the array cannot be modified.

After the slice (slice:=[]int{}) is defined, the length (len) and capacity (cap) can not be fixed!

As shown in the following figure: slice s can also append elements after re-declaration.

The above is a personal experience, I hope it can give you a reference, and I hope you can support developpaer a lot. If there are any mistakes or not considered completely, please let me know.