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

Time：2022-8-17

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 (
"errors"
"fmt"
)
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}
fmt.Println(getMissingElement(arr))
}``````

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.

## Leetcode PHP题解–D127 455. Assign Cookies

D127 455. Assign Cookies topic link 455. Assign Cookies topic analysis Given two arrays, the first array represents the capacity of each container, and the second array represents the number of each item. The items need to be stuffed into the container, but there are two conditions. Condition 1: Only one item can be stuffed […]