如何在没有Java输出排序的2个数组中打印公共元素

时间:2017-10-27 19:02:18

标签: java arrays hashset

给定两个数组,找到它们中的公共元素。

示例:[1,45,33,23,22,45,233,21], [5,23,45,0,9,23,1,9] =>输出:[1,45, 23]

import java.io.*;
import java.util.*;

class Mycode {
    public static void main(String args[]) {
        int a[] = {1, 45, 33, 23, 22, 45, 233, 21};
        int b[] = {5, 23, 45, 0, 9, 45, 1, 9};

        Mycode test = new Mycode();

        test.testNumber(a, b);
    }

    void testNumber(int c[], int d[]) {

        System.out.println(Arrays.toString(c));
        System.out.println(Arrays.toString(d));
        Set<Integer> hset = new HashSet<Integer>();

        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < d.length; j++) {
                if (c[i] == d[j]) {
                    System.out.println(c[i]);
                    hset.add(c[i]);
                }
            }
        }

        System.out.println(hset);
    }
} 

实际输出:[1, 45, 33, 23, 22, 4, 233, 21] [5, 23, 45, 0, 9, 5, 1, 9] =&gt; [1, 23, 45]

5 个答案:

答案 0 :(得分:4)

HashSet不保证JavaDoc中指示的插入顺序的保留:

  

它不保证集合的迭代顺序;在   特别是,它不保证订单将保持不变   随着时间的推移。

所以我更喜欢使用LinkedHashSet。此Set实现保证了插入顺序的保留。来自JavaDocs:

  

此链接列表定义迭代排序,即顺序   哪些元素被插入集合(插入顺序)

void testNumber(int c[], int d[]) {
    System.out.println(Arrays.toString(c));
    System.out.println(Arrays.toString(d));

    Set<Integer> hset = new LinkedHashSet<>();

    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < d.length; j++) {
            if (c[i] == d[j]) {
                hset.add(c[i]);
            }
        }
    }
    System.out.println(hset);
}

输出:

  

[1,45,23]

答案 1 :(得分:0)

你可以使用     HashMap中 循环遍历第一个数组并添加值为false的每个元素。 循环遍历第二个,如果它在hashMap中,则它是常见的。

答案 2 :(得分:0)

下面的内容怎么样?这将更加整洁,因为您不需要有两个for循环,并且您不需要事先对它进行排序。

public class Mycode {
    public static void main(String args[]) {
        Integer a[] = {1, 45, 33, 23, 22, 45, 233, 21};
        Integer b[] = {5, 23, 45, 0, 9, 45, 1, 9};

        Mycode test = new Mycode();

        System.out.println(test.testNumber(a, b));
    }

    public  <T> Set<T>  testNumber(T [] arra1, T [] arr2){
        Set<T> set1 = new HashSet<T>();
        Set<T> interset = new HashSet<T>();

        Collections.addAll(set1, arra1);
        for(T t: arr2){
            if(set1.contains(t))
                interset.add(t);
        }
        return interset;
    }

}

答案 3 :(得分:0)

        <filter>
        <filter-name>XSS</filter-name>
        <display-name>XSS</display-name>
        <description></description>
        <filter-class>com.filter.CrossScriptingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>XSS</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

答案 4 :(得分:0)

时间复杂度为N阶的解决方案

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        // write your code here

        Scanner scn = new Scanner(System.in);
        int n1 = scn.nextInt();
        int[] arr1 = new int[n1];
        for (int i = 0; i < n1; i++) {
            arr1[i] = scn.nextInt();
        }
        int n2 = scn.nextInt();
        int[] arr2 = new int[n2];
        for (int i = 0; i < n2; i++) {
            arr2[i] = scn.nextInt();
        }
        HashMap < Integer, Integer > freqMap1 = new HashMap < > ();

        for (int i = 0; i < n1; i++) {
            int ch = arr1[i];

            if (freqMap1.containsKey(ch)) {
                int f = freqMap1.get(ch);
                freqMap1.put(ch, f + 1);
            } else {
                freqMap1.put(ch, 1);
            }
        }
        for (int i = 0; i < n2; i++) {
            int ch = arr2[i];

            if (freqMap1.containsKey(ch)) {
                System.out.println(ch);
                freqMap1.remove(ch);
            }
        }

    }

}