1

Given two arrays, find the common elements in them.

Example: [1,45,33,23,22,45,233,21], [5,23,45,0,9,23,1,9] => Output: [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);
    }
} 

Actual Output: [1, 45, 33, 23, 22, 4, 233, 21] [5, 23, 45, 0, 9, 5, 1, 9] => [1, 23, 45]

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Vjay
  • 111
  • 1
  • 8
  • 1
    Possible duplicate of [How do I get the intersection between two arrays as a new array?](https://stackoverflow.com/questions/13270491/how-do-i-get-the-intersection-between-two-arrays-as-a-new-array) – icarumbas Oct 27 '17 at 19:12
  • @icarumbas I wouldn't say that this is a duplicate because it involves preserving the order of the output elements – Juan Carlos Mendoza Oct 27 '17 at 19:28

5 Answers5

4

HashSet makes no guarantees of the preservation of the insertion order as indicated in the JavaDoc:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

So I would prefer using a LinkedHashSet. This Set implementation guarantees the preservation of the insertion order. From the JavaDocs:

This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)

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);
}

Output:

[1, 45, 23]

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
0

You can use a HashMap Loop through the first array and add each element with the value false. Loop through the second and if it's in the hashMap then it's common.

tiborK
  • 385
  • 1
  • 6
0

What about something like below? This will be more neat since you don't need to have two for loops, and you don't need to sort it before hand.

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;
    }

}
Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23
0
List<Integer> list = new ArrayList<>(Arrays.asList(array1));
list.retainAll(Arrays.asList(array2));
System.out.println(list);
daniu
  • 14,137
  • 4
  • 32
  • 53
0

Solution with time complexity of order 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);
            }
        }

    }

}