Home > Backend Development > Golang > Detailed explanation of using Go language to find similarities and differences between two arrays

Detailed explanation of using Go language to find similarities and differences between two arrays

藏色散人
Release: 2021-06-02 17:07:35
forward
2931 people have browsed it

The following tutorial column of golang will introduce to you the similarities and differences of using go language to find two arrays. I hope it will be helpful to friends in need!

I recently encountered a small requirement on the project. The input is two arrays, an old array and a new array. The requirement Get all the new and deleted elements of the new array relative to the old array. For example:

输入:
arr_old: {"1", "2", "4", "5", "7", "9"}
arr_new: {"2", "3", "4", "6", "7"}
返回:
arr_added: {"3", "6"}
arr_deleted: {"1", "5", "9"}
Copy after login

There is no similar direct comparison method in the standard library of go. You need to implement it yourself. The simplest method is of course the old array. Go to the new array for each element. If it is not found, it is deleted. Then the elements of the new array are searched for in the old array one by one. If it is not found, it is added. However, this method is too inefficient.

Here I use an idea based on set operations, that is, to find the intersection and union of two arrays respectively. The union minus the intersection is all the changed elements (or new ones) , or deleted), traverse the elements in this set and search in the old array. If it is found in the old array, then it is the deleted element; otherwise, if it is not found, it must be found in the new array (use Without actually traversing it again), then it is the newly added element.

Above the code, there is a trick here, which is to take advantage of the uniqueness of the map key in go, use the elements of the array as the key of the map, and use map to achieve fast search.

package main

import (
	"fmt"
)

func main() {
	//fmt.Println("Hello World!")
	src := []string{"1", "2", "4", "5", "7", "9"}
	dest := []string{"2", "3", "4", "6", "7"}

	added, removed := Arrcmp(src, dest)
	fmt.Printf("add: %v\nrem: %v\n", added, removed)
}

func Arrcmp(src []string, dest []string) ([]string, []string) {
	msrc := make(map[string]byte) //按源数组建索引
	mall := make(map[string]byte) //源+目所有元素建索引

	var set []string //交集

	//1.源数组建立map
	for _, v := range src {
		msrc[v] = 0
		mall[v] = 0
	}
	//2.目数组中,存不进去,即重复元素,所有存不进去的集合就是并集
	for _, v := range dest {
		l := len(mall)
		mall[v] = 1
		if l != len(mall) { //长度变化,即可以存
			l = len(mall)
		} else { //存不了,进并集
			set = append(set, v)
		}
	}
	//3.遍历交集,在并集中找,找到就从并集中删,删完后就是补集(即并-交=所有变化的元素)
	for _, v := range set {
		delete(mall, v)
	}
	//4.此时,mall是补集,所有元素去源中找,找到就是删除的,找不到的必定能在目数组中找到,即新加的
	var added, deleted []string
	for v, _ := range mall {
		_, exist := msrc[v]
		if exist {
			deleted = append(deleted, v)
		} else {
			added = append(added, v)
		}
	}

	return added, deleted
}
Copy after login

Run results:

add: [6 3]
rem: [1 5 9]
Copy after login

We welcome everyone to exchange ideas for more efficient methods.

The above is the detailed content of Detailed explanation of using Go language to find similarities and differences between two arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template