Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望大佬增加一下这个func ContainsAll(slice1 []string, slice2 []string) bool #281

Open
J4sonZ opened this issue Dec 24, 2024 · 1 comment

Comments

@J4sonZ
Copy link

J4sonZ commented Dec 24, 2024

func ContainsAll(slice1 []string, slice2 []string) bool {
// 如果长度不同,则直接返回 false
if len(slice1) != len(slice2) {
return false
}

// 创建两个 map 记录每个切片中元素的频率
freq1 := make(map[string]int)
freq2 := make(map[string]int)

for _, element := range slice1 {
    freq1[element]++
}

for _, element := range slice2 {
    freq2[element]++
}

// 比较两个频率 map 是否相同
for key, count := range freq1 {
    if freq2[key] != count {
        return false
    }
}

return true

}

这个函数旨在比较两个 slice,里面的元素是否相等而不在意顺序

@duke-git
Copy link
Owner

func ContainsAll(slice1 []string, slice2 []string) bool { // 如果长度不同,则直接返回 false if len(slice1) != len(slice2) { return false }

// 创建两个 map 记录每个切片中元素的频率
freq1 := make(map[string]int)
freq2 := make(map[string]int)

for _, element := range slice1 {
    freq1[element]++
}

for _, element := range slice2 {
    freq2[element]++
}

// 比较两个频率 map 是否相同
for key, count := range freq1 {
    if freq2[key] != count {
        return false
    }
}

return true

}

这个函数旨在比较两个 slice,里面的元素是否相等而不在意顺序

@J4sonZ v2分支之前已经添加了EqualUnordered, 下个版本会发布

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants