How to find the common elements between two arrays in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        Integer arr1[] = {1, 2, 3, 4, 5, 15}; 
        Integer arr2[] = {6, 7, 8, 5, 9, 0, 2, 99}; 
     
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                  System.out.println("Common element: " + arr1[i]);
                }
            }
        }
    }
}
  
  
  
/*
run:
  
Common element: 2
Common element: 5
  
*/

 



answered Aug 18, 2021 by avibootz

Related questions

1 answer 156 views
1 answer 137 views
1 answer 152 views
1 answer 156 views
2 answers 1,121 views
3 answers 243 views
...