Array Intersection

 

You have been given two integer arrays/list(ARR1 and ARR2) of size M and N, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.


Sample Input 1 :
4
2 6 1 2
5
1 2 3 4 2
Sample Output 1 :
2 1 2

public class Solution{  
    
    public static void intersections(int arr1[], int arr2[]) {
    
        for(int i=0;i<arr1.length;i++)
        {
            for(int j=0;j<arr2.length;j++)
            {
                if(arr1[i]==arr2[j])
                {
                    System.out.print(arr1[i] + " ");
                    //values should not get repeated
                    arr2[j]=Integer.MIN_VALUE; 
                    break;

                }
            }
        }
        
        
    }
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern