首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java,寻找两个数组的交集

Java,寻找两个数组的交集
EN

Stack Overflow用户
提问于 2013-07-26 00:09:28
回答 14查看 70.1K关注 0票数 31

我已经阅读了一些关于这个的其他堆栈溢出线程:

to find the intersection of two multisets in java

How do I get the intersection between two arrays as a new array?

public static int[] intersection (int [] x, int numELementsInX, int [] y, int numElementsInY) {

我正在尝试检查两个数组以及它们的元素数(numElementsInX和numElementsInY),并返回一个包含数组x和y的公共值的新数组。它们的交集。

Example,if x is{1,3,5,7,9}and y is{9,3,9,4} then
intersection(x, 5, y, 4} should return {3, 9} or {9, 3}

我已经读到我需要使用LCS算法。有没有人能给我举个例子说明如何做到这一点?数组和数组中的值都是在另一个方法中初始化和生成的,然后传递给交集。

任何帮助/澄清都将不胜感激。

编辑代码

for (int i=0; i<numElementsInX; i++){
    for (int j=0; j<numElementsInY; j++){
        if (x[j]==x[i]) { //how to push to new array?; 
        }
        else{
        }
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17863319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档