我有两个无序的整数数组,我需要知道这些阵列有多少整数共同点 [英] I have two unordered integer arrays, and i need to know how many integers these arrays have in common

查看:87
本文介绍了我有两个无序的整数数组,我需要知道这些阵列有多少整数共同点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的LAMP环境中工作,所以PHP是语言;至少我可以使用Python。

I'm working in a LAMP environment, so PHP is the language; at least i can use python.

正如标题所说我有两个无序的整数数组。

As the title said i have two unordered integer arrays.

$array_A = array(13, 4, 59, 38, 9, 69, 72, 93, 1, 3, 5)

$array_B = array(29, 72, 21, 3, 6)

我想知道这些阵列有多少整数共同点;在这个例子中,你看到的结果是2。我不感兴趣的是什么整数共同点,像(72 3)。

I want to know how many integers these array have in common; in the example as you see the result is 2. I'm not interested in what integers are in common, like (72, 3).

我需要一个更快的方法不是采取数组b的每一个元素,并检查它是否在数组A(O(n×m个))

I need a faster method than take every element of array B and check if it's in array A ( O(nxm) )

数组可以通过ASORT或SQL顺序排序(他们从SQL结果出来)。

Arrays can be sorted through asort or with sql ordering (they came from a sql result).

这来找我的想法是创建一个载体为每一个数组,其中整数是谁得到价值1和整数并不present得到0的位置。

An idea that came to me is to create a 'vector' for every array where the integer is a position who gets value 1 and integers not present get 0.

因此​​,对于数组A(起始于POS 1)

So, for array A (starting at pos 1)

(1, 0, 1, 1, 1, 0, 0, 0, 1, 0, ...)

同样的排列B

(0, 0, 1, 0, 0, 1, ...)

和那么这两个向量与一个周期进行比较。的问题是,在这种方式的矢量长度为约40万。

And then compare this two vectors with one cycle. The problem is that in this way the vector length is about 400k.

推荐答案

最简单的方法是:

count(array_intersect($array_A, $array_B));

如果我明白你追求的。
应禁食。

if I understand what you're after. Should be fast.

这篇关于我有两个无序的整数数组,我需要知道这些阵列有多少整数共同点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆