Find all common elements in given 3 sorted arrays

Here given code implementation process.

/*
    C program for
    Find all common elements in given 3 sorted arrays
*/
#include <stdio.h>

#include <limits.h>
 // Display array elements
void printArray(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", arr[i]);
	}
}
void findCommonInTriplets(
  int arr1[], int arr2[], int arr3[], 
  int l, int m, int n)
{
	// Auxiliary variables
	int count = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	int back = INT_MIN;
	// Display given array elements
	printf("\n Array 1 \n");
	printArray(arr1, l);
	printf("\n Array 2 \n");
	printArray(arr2, m);
	printf("\n Array 3 \n");
	printArray(arr3, n);
	// Common elements
	printf("\n Common elements : ");
	while (i < l && j < m && k < n)
	{
		if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
		{
			// When common elements found
			// Check that if element is repeated or not
			if (i == 0 || arr1[i] > back)
			{
				// Display common elements
				printf(" %d", arr1[i]);
				// Avoid repeated same element
				back = arr1[i];
				count += 1;
			}
			// Increase array index
			i++;
			j++;
			k++;
		}
		else if (arr1[i] < arr2[j])
		{
			i++;
		}
		else if (arr2[j] < arr3[k])
		{
			j++;
		}
		else
		{
			k++;
		}
	}
	if (count == 0)
	{
		printf("\n None");
	}
}
int main(int argc, char const *argv[])
{
	// Given of sorted array elements
	int arr1[] = {
		-3 , 0 , 1 , 2 , 3 , 3 , 8 , 10
	};
	int arr2[] = {
		-3 , -1 , 3 , 3 , 5 , 7 , 7 , 8
	};
	int arr3[] = {
		-3 , 1 , 2 , 3 , 3 , 6 , 7 , 8 , 12
	};
	// Get the size of arrays
	int l = sizeof(arr1) / sizeof(arr1[0]);
	int m = sizeof(arr2) / sizeof(arr2[0]);
	int n = sizeof(arr3) / sizeof(arr3[0]);
	// Test
	findCommonInTriplets(arr1, arr2, arr3, l, m, n);
	return 0;
}

Output

 Array 1
  -3  0  1  2  3  3  8  10
 Array 2
  -3  -1  3  3  5  7  7  8
 Array 3
  -3  1  2  3  3  6  7  8  12
 Common elements :  -3 3 8
/*
    Java program for
    Find all common elements in given 3 sorted arrays
*/
public class CommonElements
{
    // Display array elements
    public void printArray(int[] arr, int n)
    {
        for (int i = 0; i < n; ++i)
        {
            System.out.print(" " + arr[i]);
        }
    }
    public void findCommonInTriplets(
      int[] arr1, int[] arr2, int[] arr3, 
      int l, int m, int n)
    {
        // Auxiliary variables
        int count = 0;
        int i = 0;
        int j = 0;
        int k = 0;
        int back = Integer.MIN_VALUE;
        // Display given array elements
        System.out.print("\n Array A \n");
        printArray(arr1, l);
        System.out.print("\n Array B \n");
        printArray(arr2, m);
        System.out.print("\n Array C \n");
        printArray(arr3, n);
        // Common elements
        System.out.print("\n Common elements : ");
        while (i < l && j < m && k < n)
        {
            if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
            {
                // When common elements found
                // Check that if element is repeated or not
                if (i == 0 || arr1[i] > back)
                {
                    // Display common elements
                    System.out.print(" " + arr1[i]);
                    // Avoid repeated same element
                    back = arr1[i];
                    count += 1;
                }
                // Increase array index
                i++;
                j++;
                k++;
            }
            else if (arr1[i] < arr2[j])
            {
                // Increase first array index
                i++;
            }
            else if (arr2[j] < arr3[k])
            {
                // Increase second array index
                j++;
            }
            else
            {
                // Increase third array index
                k++;
            }
        }
        if (count == 0)
        {
            System.out.print("\n None");
        }
    }
    public static void main(String[] args)
    {
        CommonElements task = new CommonElements();
        // Given of sorted array elements
        int[] arr1 = {
            -3 , 0 , 1 , 2 , 3 , 3 , 8 , 10
        };
        int[] arr2 = {
            -3 , -1 , 3 , 3 , 5 , 7 , 7 , 8
        };
        int[] arr3 = {
            -3 , 1 , 2 , 3 , 3 , 6 , 7 , 8 , 12
        };
        // Get the size of arrays
        int l = arr1.length;
        int m = arr2.length;
        int n = arr3.length;
        // Test
        task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
    }
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
// Include header file
#include <iostream>
#include <limits.h>

using namespace std;
/*
    C++ program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements
{
	public:
		// Display array elements
		void printArray(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	void findCommonInTriplets(
      int arr1[], int arr2[], int arr3[], 
      int l, int m, int n)
	{
		// Auxiliary variables
		int count = 0;
		int i = 0;
		int j = 0;
		int k = 0;
		int back = INT_MIN;
		// Display given array elements
		cout << "\n Array A \n";
		this->printArray(arr1, l);
		cout << "\n Array B \n";
		this->printArray(arr2, m);
		cout << "\n Array C \n";
		this->printArray(arr3, n);
		// Common elements
		cout << "\n Common elements : ";
		while (i < l && j < m && k < n)
		{
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1[i] > back)
				{
					// Display common elements
					cout << " " << arr1[i];
					// Avoid repeated same element
					back = arr1[i];
					count += 1;
				}
				// Increase array index
				i++;
				j++;
				k++;
			}
			else if (arr1[i] < arr2[j])
			{
				// Increase first array index
				i++;
			}
			else if (arr2[j] < arr3[k])
			{
				// Increase second array index
				j++;
			}
			else
			{
				// Increase third array index
				k++;
			}
		}
		if (count == 0)
		{
			cout << "\n None";
		}
	}
};
int main()
{
	CommonElements *task = new CommonElements();
	// Given of sorted array elements
	int arr1[] = {
		-3 , 0 , 1 , 2 , 3 , 3 , 8 , 10
	};
	int arr2[] = {
		-3 , -1 , 3 , 3 , 5 , 7 , 7 , 8
	};
	int arr3[] = {
		-3 , 1 , 2 , 3 , 3 , 6 , 7 , 8 , 12
	};
	// Get the size of arrays
	int l = sizeof(arr1) / sizeof(arr1[0]);
	int m = sizeof(arr2) / sizeof(arr2[0]);
	int n = sizeof(arr3) / sizeof(arr3[0]);
	// Test
	task->findCommonInTriplets(arr1, arr2, arr3, l, m, n);
	return 0;
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
// Include namespace system
using System;
/*
    Csharp program for
    Find all common elements in given 3 sorted arrays
*/
public class CommonElements
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void findCommonInTriplets(
      int[] arr1, int[] arr2, int[] arr3, 
      int l, int m, int n)
	{
		// Auxiliary variables
		int count = 0;
		int i = 0;
		int j = 0;
		int k = 0;
		int back = int.MinValue;
		// Display given array elements
		Console.Write("\n Array A \n");
		this.printArray(arr1, l);
		Console.Write("\n Array B \n");
		this.printArray(arr2, m);
		Console.Write("\n Array C \n");
		this.printArray(arr3, n);
		// Common elements
		Console.Write("\n Common elements : ");
		while (i < l && j < m && k < n)
		{
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1[i] > back)
				{
					// Display common elements
					Console.Write(" " + arr1[i]);
					// Avoid repeated same element
					back = arr1[i];
					count += 1;
				}
				// Increase array index
				i++;
				j++;
				k++;
			}
			else if (arr1[i] < arr2[j])
			{
				// Increase first array index
				i++;
			}
			else if (arr2[j] < arr3[k])
			{
				// Increase second array index
				j++;
			}
			else
			{
				// Increase third array index
				k++;
			}
		}
		if (count == 0)
		{
			Console.Write("\n None");
		}
	}
	public static void Main(String[] args)
	{
		CommonElements task = new CommonElements();
		// Given of sorted array elements
		int[] arr1 = {
			-3 , 0 , 1 , 2 , 3 , 3 , 8 , 10
		};
		int[] arr2 = {
			-3 , -1 , 3 , 3 , 5 , 7 , 7 , 8
		};
		int[] arr3 = {
			-3 , 1 , 2 , 3 , 3 , 6 , 7 , 8 , 12
		};
		// Get the size of arrays
		int l = arr1.Length;
		int m = arr2.Length;
		int n = arr3.Length;
		// Test
		task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
	}
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
package main
import "math"
import "fmt"
/*
    Go program for
    Find all common elements in given 3 sorted arrays
*/

// Display array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func findCommonInTriplets(arr1[] int, 
	arr2[] int, arr3[] int, 
	l int, m int, n int) {
	// Auxiliary variables
	var count int = 0
	var i int = 0
	var j int = 0
	var k int = 0
	var back int = math.MinInt64
	// Display given array elements
	fmt.Print("\n Array A \n")
	printArray(arr1, l)
	fmt.Print("\n Array B \n")
	printArray(arr2, m)
	fmt.Print("\n Array C \n")
	printArray(arr3, n)
	// Common elements
	fmt.Print("\n Common elements : ")
	for (i < l && j < m && k < n) {
		if arr1[i] == arr2[j] && arr2[j] == arr3[k] {
			// When common elements found
			// Check that if element is repeated or not
			if i == 0 || arr1[i] > back {
				// Display common elements
				fmt.Print(" ", arr1[i])
				// Avoid repeated same element
				back = arr1[i]
				count += 1
			}
			// Increase array index
			i++
			j++
			k++
		} else if arr1[i] < arr2[j] {
			// Increase first array index
			i++
		} else if arr2[j] < arr3[k] {
			// Increase second array index
			j++
		} else {
			// Increase third array index
			k++
		}
	}
	if count == 0 {
		fmt.Print("\n None")
	}
}
func main() {

	// Given of sorted array elements
	var arr1 = [] int { -3, 0, 1, 2, 3, 3, 8, 10 }
	var arr2 = [] int { -3, -1, 3, 3, 5, 7, 7, 8 }
	var arr3 = [] int { -3, 1, 2, 3, 3, 6, 7, 8, 12 }
	// Get the size of arrays
	var l int = len(arr1)
	var m int = len(arr2)
	var n int = len(arr3)
	// Test
	findCommonInTriplets(arr1, arr2, arr3, l, m, n)
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
<?php
/*
    Php program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements
{
	// Display array elements
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function findCommonInTriplets(
    $arr1, $arr2, $arr3, $l, $m, $n)
	{
		// Auxiliary variables
		$count = 0;
		$i = 0;
		$j = 0;
		$k = 0;
		$back = -PHP_INT_MAX;
		// Display given array elements
		echo("\n Array A \n");
		$this->printArray($arr1, $l);
		echo("\n Array B \n");
		$this->printArray($arr2, $m);
		echo("\n Array C \n");
		$this->printArray($arr3, $n);
		// Common elements
		echo("\n Common elements : ");
		while ($i < $l && $j < $m && $k < $n)
		{
			if ($arr1[$i] == $arr2[$j] && $arr2[$j] == $arr3[$k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if ($i == 0 || $arr1[$i] > $back)
				{
					// Display common elements
					echo(" ".$arr1[$i]);
					// Avoid repeated same element
					$back = $arr1[$i];
					$count += 1;
				}
				// Increase array index
				$i++;
				$j++;
				$k++;
			}
			else if ($arr1[$i] < $arr2[$j])
			{
				// Increase first array index
				$i++;
			}
			else if ($arr2[$j] < $arr3[$k])
			{
				// Increase second array index
				$j++;
			}
			else
			{
				// Increase third array index
				$k++;
			}
		}
		if ($count == 0)
		{
			echo("\n None");
		}
	}
}

function main()
{
	$task = new CommonElements();
	// Given of sorted array elements
	$arr1 = array(-3, 0, 1, 2, 3, 3, 8, 10);
	$arr2 = array(-3, -1, 3, 3, 5, 7, 7, 8);
	$arr3 = array(-3, 1, 2, 3, 3, 6, 7, 8, 12);
	// Get the size of arrays
	$l = count($arr1);
	$m = count($arr2);
	$n = count($arr3);
	// Test
	$task->findCommonInTriplets($arr1, $arr2, $arr3, $l, $m, $n);
}
main();

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
/*
    Node JS program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements
{
	// Display array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	findCommonInTriplets(arr1, arr2, arr3, l, m, n)
	{
		// Auxiliary variables
		var count = 0;
		var i = 0;
		var j = 0;
		var k = 0;
		var back = -Number.MAX_VALUE;
		// Display given array elements
		process.stdout.write("\n Array A \n");
		this.printArray(arr1, l);
		process.stdout.write("\n Array B \n");
		this.printArray(arr2, m);
		process.stdout.write("\n Array C \n");
		this.printArray(arr3, n);
		// Common elements
		process.stdout.write("\n Common elements : ");
		while (i < l && j < m && k < n)
		{
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1[i] > back)
				{
					// Display common elements
					process.stdout.write(" " + arr1[i]);
					// Avoid repeated same element
					back = arr1[i];
					count += 1;
				}
				// Increase array index
				i++;
				j++;
				k++;
			}
			else if (arr1[i] < arr2[j])
			{
				// Increase first array index
				i++;
			}
			else if (arr2[j] < arr3[k])
			{
				// Increase second array index
				j++;
			}
			else
			{
				// Increase third array index
				k++;
			}
		}
		if (count == 0)
		{
			process.stdout.write("\n None");
		}
	}
}

function main()
{
	var task = new CommonElements();
	// Given of sorted array elements
	var arr1 = [-3, 0, 1, 2, 3, 3, 8, 10];
	var arr2 = [-3, -1, 3, 3, 5, 7, 7, 8];
	var arr3 = [-3, 1, 2, 3, 3, 6, 7, 8, 12];
	// Get the size of arrays
	var l = arr1.length;
	var m = arr2.length;
	var n = arr3.length;
	// Test
	task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
}
main();

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
import sys
#    Python 3 program for
#    Find all common elements in given 3 sorted arrays
class CommonElements :
	#  Display list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def findCommonInTriplets(self, arr1, arr2, arr3, l, m, n) :
		#  Auxiliary variables
		count = 0
		i = 0
		j = 0
		k = 0
		back = -sys.maxsize
		#  Display given list elements
		print("\n Array A ")
		self.printArray(arr1, l)
		print("\n Array B ")
		self.printArray(arr2, m)
		print("\n Array C ")
		self.printArray(arr3, n)
		#  Common elements
		print("\n Common elements : ", end = "")
		while (i < l and j < m and k < n) :
			if (arr1[i] == arr2[j] and arr2[j] == arr3[k]) :
				#  When common elements found
				#  Check that if element is repeated or not
				if (i == 0 or arr1[i] > back) :
					#  Display common elements
					print(" ", arr1[i], end = "")
					#  Avoid repeated same element
					back = arr1[i]
					count += 1
				
				#  Increase list index
				i += 1
				j += 1
				k += 1
			elif (arr1[i] < arr2[j]) :
				#  Increase first list index
				i += 1
			elif (arr2[j] < arr3[k]) :
				#  Increase second list index
				j += 1
			else :
				#  Increase third list index
				k += 1
			
		
		if (count == 0) :
			print("\n None", end = "")
		
	

def main() :
	task = CommonElements()
	#  Given of sorted list elements
	arr1 = [-3, 0, 1, 2, 3, 3, 8, 10]
	arr2 = [-3, -1, 3, 3, 5, 7, 7, 8]
	arr3 = [-3, 1, 2, 3, 3, 6, 7, 8, 12]
	#  Get the size of lists
	l = len(arr1)
	m = len(arr2)
	n = len(arr3)
	#  Test
	task.findCommonInTriplets(arr1, arr2, arr3, l, m, n)

if __name__ == "__main__": main()

Output

 Array A
  -3  0  1  2  3  3  8  10
 Array B
  -3  -1  3  3  5  7  7  8
 Array C
  -3  1  2  3  3  6  7  8  12
 Common elements :   -3  3  8
#    Ruby program for
#    Find all common elements in given 3 sorted arrays
class CommonElements 
	#  Display array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def findCommonInTriplets(arr1, arr2, arr3, l, m, n) 
		#  Auxiliary variables
		count = 0
		i = 0
		j = 0
		k = 0
		back = -(2 ** (0. size * 8 - 2))
		#  Display given array elements
		print("\n Array A \n")
		self.printArray(arr1, l)
		print("\n Array B \n")
		self.printArray(arr2, m)
		print("\n Array C \n")
		self.printArray(arr3, n)
		#  Common elements
		print("\n Common elements : ")
		while (i < l && j < m && k < n) 
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) 
				#  When common elements found
				#  Check that if element is repeated or not
				if (i == 0 || arr1[i] > back) 
					#  Display common elements
					print(" ", arr1[i])
					#  Avoid repeated same element
					back = arr1[i]
					count += 1
				end

				#  Increase array index
				i += 1
				j += 1
				k += 1
			elsif (arr1[i] < arr2[j]) 
				#  Increase first array index
				i += 1
			elsif (arr2[j] < arr3[k]) 
				#  Increase second array index
				j += 1
			else
 
				#  Increase third array index
				k += 1
			end

		end

		if (count == 0) 
			print("\n None")
		end

	end

end

def main() 
	task = CommonElements.new()
	#  Given of sorted array elements
	arr1 = [-3, 0, 1, 2, 3, 3, 8, 10]
	arr2 = [-3, -1, 3, 3, 5, 7, 7, 8]
	arr3 = [-3, 1, 2, 3, 3, 6, 7, 8, 12]
	#  Get the size of arrays
	l = arr1.length
	m = arr2.length
	n = arr3.length
	#  Test
	task.findCommonInTriplets(arr1, arr2, arr3, l, m, n)
end

main()

Output

 Array A 
 -3 0 1 2 3 3 8 10
 Array B 
 -3 -1 3 3 5 7 7 8
 Array C 
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
/*
    Scala program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements()
{
	// Display array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def findCommonInTriplets(
      arr1: Array[Int], arr2: Array[Int], arr3: Array[Int], 
      l: Int, m: Int, n: Int): Unit = {
		// Auxiliary variables
		var count: Int = 0;
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var back: Int = Int.MinValue;
		// Display given array elements
		print("\n Array A \n");
		printArray(arr1, l);
		print("\n Array B \n");
		printArray(arr2, m);
		print("\n Array C \n");
		printArray(arr3, n);
		// Common elements
		print("\n Common elements : ");
		while (i < l && j < m && k < n)
		{
			if (arr1(i) == arr2(j) && arr2(j) == arr3(k))
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1(i) > back)
				{
					// Display common elements
					print(" " + arr1(i));
					// Avoid repeated same element
					back = arr1(i);
					count += 1;
				}
				// Increase array index
				i += 1;
				j += 1;
				k += 1;
			}
			else if (arr1(i) < arr2(j))
			{
				// Increase first array index
				i += 1;
			}
			else if (arr2(j) < arr3(k))
			{
				// Increase second array index
				j += 1;
			}
			else
			{
				// Increase third array index
				k += 1;
			}
		}
		if (count == 0)
		{
			print("\n None");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CommonElements = new CommonElements();
		// Given of sorted array elements
		var arr1: Array[Int] = Array(-3, 0, 1, 2, 3, 3, 8, 10);
		var arr2: Array[Int] = Array(-3, -1, 3, 3, 5, 7, 7, 8);
		var arr3: Array[Int] = Array(-3, 1, 2, 3, 3, 6, 7, 8, 12);
		// Get the size of arrays
		var l: Int = arr1.length;
		var m: Int = arr2.length;
		var n: Int = arr3.length;
		// Test
		task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
	}
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8
import Foundation;
/*
    Swift 4 program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements
{
	// Display array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func findCommonInTriplets(
      _ arr1: [Int], _ arr2: [Int], _ arr3: [Int], 
      _ l: Int, _ m: Int, _ n: Int)
	{
		// Auxiliary variables
		var count: Int = 0;
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var back: Int = Int.min;
		// Display given array elements
		print("\n Array A ");
		self.printArray(arr1, l);
		print("\n Array B ");
		self.printArray(arr2, m);
		print("\n Array C ");
		self.printArray(arr3, n);
		// Common elements
		print("\n Common elements : ", terminator: "");
		while (i < l && j < m && k < n)
		{
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1[i] > back)
				{
					// Display common elements
					print(" ", arr1[i], terminator: "");
					// Avoid repeated same element
					back = arr1[i];
					count += 1;
				}
				// Increase array index
				i += 1;
				j += 1;
				k += 1;
			}
			else if (arr1[i] < arr2[j])
			{
				// Increase first array index
				i += 1;
			}
			else if (arr2[j] < arr3[k])
			{
				// Increase second array index
				j += 1;
			}
			else
			{
				// Increase third array index
				k += 1;
			}
		}
		if (count == 0)
		{
			print("\n None", terminator: "");
		}
	}
}
func main()
{
	let task: CommonElements = CommonElements();
	// Given of sorted array elements
	let arr1: [Int] = [-3, 0, 1, 2, 3, 3, 8, 10];
	let arr2: [Int] = [-3, -1, 3, 3, 5, 7, 7, 8];
	let arr3: [Int] = [-3, 1, 2, 3, 3, 6, 7, 8, 12];
	// Get the size of arrays
	let l: Int = arr1.count;
	let m: Int = arr2.count;
	let n: Int = arr3.count;
	// Test
	task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
}
main();

Output

 Array A
  -3  0  1  2  3  3  8  10
 Array B
  -3  -1  3  3  5  7  7  8
 Array C
  -3  1  2  3  3  6  7  8  12
 Common elements :   -3  3  8
/*
    Kotlin program for
    Find all common elements in given 3 sorted arrays
*/
class CommonElements
{
	// Display array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun findCommonInTriplets(
      arr1: Array < Int > , arr2: Array < Int > , arr3: Array < Int > , 
      l: Int, m: Int, n: Int): Unit
	{
		// Auxiliary variables
		var count: Int = 0;
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var back: Int = Int.MIN_VALUE;
		// Display given array elements
		print("\n Array A \n");
		this.printArray(arr1, l);
		print("\n Array B \n");
		this.printArray(arr2, m);
		print("\n Array C \n");
		this.printArray(arr3, n);
		// Common elements
		print("\n Common elements : ");
		while (i < l && j < m && k < n)
		{
			if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
			{
				// When common elements found
				// Check that if element is repeated or not
				if (i == 0 || arr1[i] > back)
				{
					// Display common elements
					print(" " + arr1[i]);
					// Avoid repeated same element
					back = arr1[i];
					count += 1;
				}
				// Increase array index
				i += 1;
				j += 1;
				k += 1;
			}
			else if (arr1[i] < arr2[j])
			{
				// Increase first array index
				i += 1;
			}
			else if (arr2[j] < arr3[k])
			{
				// Increase second array index
				j += 1;
			}
			else
			{
				// Increase third array index
				k += 1;
			}
		}
		if (count == 0)
		{
			print("\n None");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CommonElements = CommonElements();
	// Given of sorted array elements
	val arr1: Array < Int > = arrayOf(-3, 0, 1, 2, 3, 3, 8, 10);
	val arr2: Array < Int > = arrayOf(-3, -1, 3, 3, 5, 7, 7, 8);
	val arr3: Array < Int > = arrayOf(-3, 1, 2, 3, 3, 6, 7, 8, 12);
	// Get the size of arrays
	val l: Int = arr1.count();
	val m: Int = arr2.count();
	val n: Int = arr3.count();
	// Test
	task.findCommonInTriplets(arr1, arr2, arr3, l, m, n);
}

Output

 Array A
 -3 0 1 2 3 3 8 10
 Array B
 -3 -1 3 3 5 7 7 8
 Array C
 -3 1 2 3 3 6 7 8 12
 Common elements :  -3 3 8


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved