Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,866 questions

51,788 answers

573 users

How to find all common elements in given three sorted arrays with Java

1 Answer

0 votes
public class MyClass {
    private static void PrintCommonElementsInThreeArrays(int[] arr1, int[] arr2, int[] arr3) {
    	int size1 = arr1.length;
    	int size2 = arr2.length;
    	int size3 = arr3.length;
    	
    	int i = 0, j = 0, k = 0;
    
    	while (i < size1 && j < size2 && k < size3)	{
    		if (arr1[i] == arr2[j] && arr3[k] == arr1[i]) {
    			System.out.print(arr1[i] + " ");
    			i++; j++; k++;
    		}
    		else if (arr1[i] < arr2[j]) {
    				i++;
    		    }
    		else if (arr2[j] < arr3[k])	{
    				j++;
    		    }
    		else {
    			k++;
    		}
    	}
    }
    public static void main(String args[]) {
      	int[] arr1 = {2, 5, 6, 7, 9, 12, 20, 25, 30, 31};
	    int[] arr2 = {4, 7, 10, 11, 20, 21, 30, 31, 37};
	    int[] arr3 = {1, 2, 5, 7, 9, 18, 19, 20, 31, 32, 38, 39, 40, 50};

	    PrintCommonElementsInThreeArrays(arr1, arr2, arr3);
    }
}





/*
run:
 
7 20 31 
 
*/

 



answered Sep 21, 2022 by avibootz

Related questions

...