java arrays contain_java:如何检查两个数组是否包含公共元素?(java: how to check if two arrays contain common elements?...

java:如何检查两个数组是否包含公共元素?(java: how to check if two arrays contain common elements? [closed])

有没有简单的方法来检查两个数组是否包含任何公共元素? 这个合适吗? 数组包含char类型。

Arrays.asList(encryptU).contains(Ualpha[randNum]));

提前致谢!

Is there any easy way to check if two arrays contain any common elements? Is this appropriate? The arrays contain type char.

Arrays.asList(encryptU).contains(Ualpha[randNum]));

thanks in advance!

原文:https://stackoverflow.com/questions/12765271

更新时间:2020-02-01 21:52

最满意答案

如果数组很小,那么带有嵌套for循环的解决方案(例如@ scaryrawr)将表现最佳。

如果阵列足够大,那么上述解决方案的O(N^2)复杂度将是有问题的。 解决方案是使用HashSet; 例如

HashSet tmp = new HashSet();

for (char ch : arr1) {

tmp.add(ch);

}

for (char ch : arr2) {

if (tmp.contains(ch)) {

// elements in common!!

}

}

这是O(N)的时间,虽然比例常数相当大。 (我认为你需要数组大小为20或30的产品比嵌套循环解决方案更快......但这是猜测。)此外,这需要O(N)临时空间。

如果字符的范围有限,那么您可以使用BitSet而不是HashSet 。 这也将在时间和空间上大致为O(N) ,尽管字符的范围也是复杂性的一个因素,因此将其称为O(N)是过度简化。

但我们可能“过度思考”这一点。 最好的建议可能是实现简单的操作,如果怀疑性能是一个真正的问题,那么对其进行分析以避免浪费你的时间进行不必要的优化。

If the arrays are small, then a solution with a nested for loop (e.g. @scaryrawr's) is going to perform best.

If the arrays are large enough, then the O(N^2) complexity of the above solution will be problematic. The solution is to use a HashSet; e.g.

HashSet tmp = new HashSet();

for (char ch : arr1) {

tmp.add(ch);

}

for (char ch : arr2) {

if (tmp.contains(ch)) {

// elements in common!!

}

}

This is O(N) in time, though the constant of proportionality is rather large. (I think you'd need the product of the array sizes to be 20 or 30 for this to be faster than the nested loop solution ... but that is a guess.) Also, this requires O(N) temporary space.

If the range of the characters is limited, then you could use a BitSet instead of a HashSet. That will also be roughly O(N) in time and space, though the range of the characters is also a factor in the complexity, so calling it O(N) is an over-simplification.

But we are probably "over-thinking" this. The best advice would probably be to implement something simple, and if there is a suspicion that performance is a real concern then profile it to avoid wasting your time with unnecessary optimizing.

2012-10-07

相关问答

像这样做: function check_if_exists($company_timings,$in_time) {

$length_of_company=sizeof($company_timings);

$length_of_emp=sizeof($in_time);

for ($i=0; $i <=$length_of_company-1 ; $i++) {

# code...

for ($j=0; $j <=$length

...

假设你的匹配序列中有“洞”(A = [1,3,2] AND B = [1,4,2]那么MatchSet = {1,2}) 也许我错了,但你可以尝试这个伪代码: i

matchSet

While i < Length(A) AND j < Length(B):

If A[i] == B[j] Then

matchSet.add(A[i])

i

jHit

...

使用jQuery 您可以使用jQuery比较两个数组: // example arrays:

var firstArray = [ 1, 2, 3, 4, 5 ];

var secondArray = [ 5, 4, 3, 2, 1 ];

// compare arrays:

var isSameSet = function( arr1, arr2 ) {

return $( arr1 ).not( arr2 ).length === 0 && $( arr2 ).not( arr1 )

...

假设输入数组是A和B ,你可以使用np.in1d和np.any一样, import numpy as np

np.in1d(A,B).any()

您也可以使用NumPy's broadcasting capability , (A.ravel()[:,None] == B.ravel()).any()

Assuming the input arrays to be A and B, you can use np.in1d with np.any, like so - import numpy

...

注意我必须创建一个数组来包含各种数组的长度,以及一个参数来告诉该数组的长度。 #include

#include

#include

void print_array(int *a, int size) {

int i;

for (i = 0; i

printf("%d ", a[i]);

}

printf("\n");

}

bool is_in(int

...

您可以使用$setIntersection为$setIntersection查询尝试$redact 。 $setIntersection将FirstArray的Name与FirstArray的Name进行比较,并返回通用名称文档的数组,后跟$size和$redact ,并将结果与0进行比较以保留并除去文档。 db.collection.aggregate(

[{

$redact: {

$cond: {

if: {

$eq: [{

...

没有这样的内置功能 你可以用它 angular.forEach(array1, function(value, key) {

angular.forEach(array2, function(value_1, key_1) {

if (value === value_1) {

// condition or action

}

});

}); There is no such built-in function you can use this angular.fo

...

一种方法是循环访问一个数组,并使用$ .inArray作为第二个数组。 function hasCommonElement(arr1,arr2)

{

var bExists = false;

$.each(arr2, function(index, value){

if($.inArray(value,arr1)!=-1){

console.log(value);

bExists = true;

}

if(bExists

...

如果数组很小,那么带有嵌套for循环的解决方案(例如@ scaryrawr)将表现最佳。 如果阵列足够大,那么上述解决方案的O(N^2)复杂度将是有问题的。 解决方案是使用HashSet; 例如 HashSet tmp = new HashSet();

for (char ch : arr1) {

tmp.add(ch);

}

for (char ch : arr2) {

if (tmp.contains(ch)) {

/

...

HashSet不保证JavaDoc中指示的插入顺序的保留: 它不保证集合的迭代顺序; 特别是,它不保证订单会随着时间的推移保持不变。 所以我更喜欢使用LinkedHashSet 。 此Set实现保证了插入顺序的保留。 来自JavaDocs: 此链接列表定义迭代排序,即元素插入集合的顺序(插入顺序) void testNumber(int c[], int d[]) {

System.out.println(Arrays.toString(c));

System.out.printl

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值