两个数组查找公共部分算法

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

/**

 * 

 * 

 * 查找两个数组中的公共部分

 */

public class SameString {

public static void main(String[] args) {

String s1[] = { “a”, “b”, “c” };

String s2[] = { “a”, “d”, “hh”, “b” };

String result[] = new String[s1.length + s2.length];

System.arraycopy(s1, 0, result, 0, s1.length);

System.arraycopy(s2, 0, result, s1.length, s2.length);

Arrays.sort(result);

Set<String> need = new HashSet<String>();

int times = 0;

for (int i = 0; i < result.length; i++) {

times++;

if (i+1 < result.length) {

if (result[i].equals(result[i + 1])) {

need.add(result[i]);

}

}

}

System.out.println(times);

for(String i: need){

System.out.println(i);

}

}

}

    原文作者:查找算法
    原文地址: https://blog.csdn.net/BloodyCoder/article/details/6610857
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞