如何在另一个数组中找到相交的元素。

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

How to find an element intersect in other array

问题

你好!根据你提供的内容,你想知道如何在不使用 foreach 循环的情况下判断数组 b 是否包含数组 a 中的元素。

你可以使用集合的交集操作来实现这个目的。首先,将数组 a 和数组 b 转换为集合,然后使用集合的交集操作,如果交集不为空,则表示数组 b 包含数组 a 中的元素。

以下是示例代码:

a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]

set_a = set(a)
set_b = set(b)

intersection = set_a.intersection(set_b)

if intersection:
    print("数组 b 包含数组 a 中的元素")
else:
    print("数组 b 不包含数组 a 中的元素")

希望对你有帮助!如果还有其他问题,请随时提问。

英文:

<br>
I have an array like: <br>

a:= [1,2,3,4,5]
b:= [5,6,7,8,9]

How to know array b have contain element in array a without using foreach?

答案1

得分: 3

如何在不使用 foreach 循环的情况下判断数组 b 是否包含数组 a 中的元素?

你无法做到这一点,也不应该尝试,因为这是一个没有意义的限制。

英文:

> How to know array b have contain element in array a without using foreach?

You can't. And you should not try as this is pointless restriction.

答案2

得分: 1

如果数组已经排序(就像你问题中的情况),有一种比逐个元素遍历更好的算法。

选择数组 a 的第一个元素,称为 x
在数组 b 中进行二分查找,找到第一个大于等于 x 的元素。如果它们相等,说明找到了两个数组中都包含的元素;如果不相等,将该元素作为新的 x。然后以同样的方式在数组 a 中查找 x。重复这个过程,直到其中一个数组的元素用完。

这个算法可以轻松地扩展到任意数量的数组(实际上,使用任意数量的数组更容易编写)。

以下是一个简单而粗糙的实现:

package main

import (
	"fmt"
	"sort"
)

func inter(arrs ...[]int) []int {
	res := []int{}
	x := arrs[0][0]
	i := 1
	for {
		off := sort.SearchInts(arrs[i], x)
		if off == len(arrs[i]) {
			// 一个切片已经为空,结束循环。
			break
		}
		if arrs[i][off] == x {
			i++
			if i == len(arrs) {
				// x 在所有切片中都存在
				res = append(res, x)
				x++ // 寻找下一个可能的 x。
				i = 0
			}
		} else {
			x = arrs[i][off]
			i = 0 // 这一步可以更加优化。
		}
	}
	return res
}

func main() {
	a := []int{1, 2, 3, 4, 5, 7}
	b := []int{5, 6, 7, 8, 9}

	fmt.Println(inter(a, b))
}

希望对你有帮助!

英文:

If the arrays are sorted (as they appear to be in your question) there is an algorithm that works better than going through each element.

Pick the first element of a, call it x.
Binary search b for the first element equal or greater than x. If they are equal, you found an element that is contained in both arrays, if not, make that your new x. Now search a for x in the same way. Repeat until you run out of elements in one of the arrays.

This can be trivially extended to an arbitrary number of arrays (in fact, it's easier to write with an arbitrary number of arrays).

Here's a quick and dirty implementation:

package main

import (
	&quot;fmt&quot;
	&quot;sort&quot;
)

func inter(arrs ...[]int) []int {
	res := []int{}
	x := arrs[0][0]
	i := 1
	for {
		off := sort.SearchInts(arrs[i], x)
		if off == len(arrs[i]) {
			// we emptied one slice, we&#39;re done.
			break
		}
		if arrs[i][off] == x {
			i++
			if i == len(arrs) {
				// x was in all the slices
				res = append(res, x)
				x++ // search for the next possible x.
				i = 0
			}
		} else {
			x = arrs[i][off]
			i = 0 // This can be done a bit more optimally.
		}
	}
	return res
}

func main() {
	a := []int{1, 2, 3, 4, 5, 7}
	b := []int{5, 6, 7, 8, 9}

	fmt.Println(inter(a, b))
}

答案3

得分: 0

package main

import (
    set "github.com/deckarep/golang-set"
)

func array_intersect(a, b []interface{}) []interface{} {
    return set.NewSetFromSlice(a).Intersect(set.NewSetFromSlice(b)).ToSlice()
}

func main() {
    a := []interface{}{1, 2, 3, 4, 5, 7}
    b := []interface{}{5, 6, 7, 8, 9}

    println(array_intersect(a, b))
}
package main

import (
    set "github.com/deckarep/golang-set"
)

func array_intersect(a, b []interface{}) []interface{} {
    return set.NewSetFromSlice(a).Intersect(set.NewSetFromSlice(b)).ToSlice()
}

func main() {
    a := []interface{}{1, 2, 3, 4, 5, 7}
    b := []interface{}{5, 6, 7, 8, 9}

    println(array_intersect(a, b))
}
英文:
package main

import (
	set &quot;github.com/deckarep/golang-set&quot;
)

func array_intersect(a, b []interface{}) []interface{} {
	return set.NewSetFromSlice(a).Intersect(set.NewSetFromSlice(b)).ToSlice()
}
func main() {
	a := []interface{}{1, 2, 3, 4, 5, 7}
	b := []interface{}{5, 6, 7, 8, 9}

	println(array_intersect(a, b))
}

答案4

得分: 0

package main

import (
	"fmt"
	"sort"
)

func array_intersect(a, b []int) []int {
	ret := []int{}

	lenA := len(a)
	lenB := len(b)
	if lenA == 0 || lenB == 0 {
		return ret
	}
	sort.Ints(a)
	sort.Ints(b)
	var i, j int
	for {
		a = a[i:]
		if i = sort.SearchInts(a, b[j]); i >= len(a) {
			break
		}
		if a[i] == b[j] {
			ret = append(ret, a[i])
		}
		if j++; j >= lenB {
			break
		}
	}

	return ret
}
func main() {
	a := []int{5, 7, 1, 1, 2, 3, 4, 5, 7}
	b := []int{1, 1, 5, 6, 7, 8, 9}
	fmt.Printf("a=%v, b=%v", a, b)
	fmt.Printf("%v\n", array_intersect(a, b))
	fmt.Printf("a=%v, b=%v", a, b)
}
package main

import (
	"fmt"
	"sort"
)

func array_intersect(a, b []int) []int {
	ret := []int{}

	lenA := len(a)
	lenB := len(b)
	if lenA == 0 || lenB == 0 {
		return ret
	}
	sort.Ints(a)
	sort.Ints(b)
	var i, j int
	for {
		a = a[i:]
		if i = sort.SearchInts(a, b[j]); i >= len(a) {
			break
		}
		if a[i] == b[j] {
			ret = append(ret, a[i])
		}
		if j++; j >= lenB {
			break
		}
	}

	return ret
}
func main() {
	a := []int{5, 7, 1, 1, 2, 3, 4, 5, 7}
	b := []int{1, 1, 5, 6, 7, 8, 9}
	fmt.Printf("a=%v, b=%v", a, b)
	fmt.Printf("%v\n", array_intersect(a, b))
	fmt.Printf("a=%v, b=%v", a, b)
}
英文:
package main

import (
	&quot;fmt&quot;
	&quot;sort&quot;
)

func array_intersect(a, b []int) []int {
	ret := []int{}

	lenA := len(a)
	lenB := len(b)
	if lenA == 0 || lenB == 0 {
		return ret
	}
	sort.Ints(a)
	sort.Ints(b)
	var i, j int
	for {
		a = a[i:]
		if i = sort.SearchInts(a, b[j]); i &gt;= len(a) {
			break
		}
		if a[i] == b[j] {
			ret = append(ret, a[i])
		}
		if j++; j &gt;= lenB {
			break
		}
	}

	return ret
}
func main() {
	a := []int{5, 7, 1, 1, 2, 3, 4, 5, 7}
	b := []int{1, 1, 5, 6, 7, 8, 9}
	fmt.Printf(&quot;a=%v, b=%v&quot;, a, b)
	fmt.Printf(&quot;%v\n&quot;, array_intersect(a, b))
	fmt.Printf(&quot;a=%v, b=%v&quot;, a, b)
}

huangapple
  • 本文由 发表于 2017年6月21日 16:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/44670855.html
匿名

发表评论

匿名网友

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

确定