Skip to main content

Find the union of two sorted arrays

Here given code implementation process.

// C Program
// Find the union of two sorted arrays
#include <stdio.h>

//Display elements of given record
void display(int record[], int size)
{
    for (int i = 0; i < size; i++)
    {
        printf(" %d ", record[i]);
    }
}
//Check that whether location element already exists in given record
int is_already_exist(int record[], int location)
{
    if (location > 0 && record[location] == record[location - 1])
    {
        return 1;
    }
    return 0;
}
//Find the union of given two sorted order sets (array or list)
void find_union(int set1[], int set2[], int n, int m)
{
    //Loop controlling variables
    int i = 0;
    int j = 0;
    printf("\n Set A : ");
    display(set1, n);
    printf("\n Set B : ");
    display(set2, m);
    printf("\n(A U B) : ");
    while (i < n || j < m)
    {
        //Check i and j length are valid to given sets or not
        if (i < n && j < m)
        {
            //When [i] and [j] location element exist in given sets 
            if (is_already_exist(set1, i) == 1)
            {
                //when set-1 [i] location element are already exist
                i++;
            }
            else if (is_already_exist(set2, j) == 1)
            {
                //when set-2 [j] location element are already exist
                j++;
            }
            else if (j > 0 && set1[i] == set2[j - 1])
            {
                //when set-1 [i] location element are already exist in set 2
                i++;
            }
            else if (i > 0 && set2[j] == set1[i - 1])
            {
                //when set-2 [j] location element are already exist in set 1
                j++;
            }
            else if (set1[i] == set2[j])
            {
                //when get a new common unique elements
                printf("  %d", set1[i]);
                i++;
                j++;
            }
            else if (set1[i] < set2[j])
            {
                printf("  %d", set1[i]);
                i++;
            }
            else if (set1[i] > set2[j])
            {
                printf("  %d", set2[j]);
                j++;
            }
        }
        else if (i < n)
        {
            if (is_already_exist(set1, i) == 0)
            {
                //Check that current element exist in other set
                if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
                {
                    //When element are not exist in set2
                    printf("  %d", set1[i]);
                }
            }
            i++;
        }
        else
        {
            if (is_already_exist(set2, j) == 0)
            {
                //Check that current element exist in other set
                if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
                {
                    //When element are not exist in set1
                    printf("  %d", set2[j]);
                }
            }
            j++;
        }
    }
    printf("\n");
}
int main()
{
    //Define sorted elements sets
    int set1[] = {
        1 , 2 , 2 , 5 , 6 , 6, 8
    };
    int set2[] = {
        1 , 2 , 2 , 2 , 3 , 3 , 6 , 6
    };
    //Get the size
    int n = sizeof(set1) / sizeof(set1[0]);
    int m = sizeof(set2) / sizeof(set2[0]);
    // 1 2 3 5 6 8
    find_union(set1, set2, n, m);
    int set3[] = {
        2 , 7 , 8 , 8 , 9
    };
    int set4[] = {
        3 , 5 , 6 , 7 , 7 , 7
    };
    //Get the size
    n = sizeof(set3) / sizeof(set3[0]);
    m = sizeof(set4) / sizeof(set4[0]);
    // 2 3 5 6 7 8 9
    find_union(set3, set4, n, m);
    int set5[] = {
        1 , 2
    };
    int set6[] = {
        8 , 9 , 11
    };
    //Get the size
    n = sizeof(set5) / sizeof(set5[0]);
    m = sizeof(set6) / sizeof(set6[0]);
    find_union(set5, set6, n, m);
    return 0;
}

Output

 Set A :  1  2  2  5  6  6  8
 Set B :  1  2  2  2  3  3  6  6
(A U B) :   1  2  3  5  6  8

 Set A :  2  7  8  8  9
 Set B :  3  5  6  7  7  7
(A U B) :   2  3  5  6  7  8  9

 Set A :  1  2
 Set B :  8  9  11
(A U B) :   1  2  8  9  11
// Java program
// Find the union of two sorted arrays
class FindUnions
{
	//Display elements of given record
	public void display(int[] record, int size)
	{
		for (int i = 0; i < size; i++)
		{
			System.out.print("  " + record[i]);
		}
	}
	//Check that whether location element already exists in given record
	public boolean is_already_exist(int[] record, int location)
	{
		if (location > 0 && record[location] == record[location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	public void find_union(int[] set1, int[] set2, int n, int m)
	{
		//Loop controlling variables
		int i = 0;
		int j = 0;
		System.out.print("\n Set A : ");
		display(set1, n);
		System.out.print("\n Set B : ");
		display(set2, m);
		System.out.print("\n Union : ");
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i++;
				}
				else if (is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j++;
				}
				else if (j > 0 && set1[i] == set2[j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					i++;
				}
				else if (i > 0 && set2[j] == set1[i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					j++;
				}
				else if (set1[i] == set2[j])
				{
					//when get a new common unique elements
					System.out.print("  " + set1[i]);
					i++;
					j++;
				}
				else if (set1[i] < set2[j])
				{
					System.out.print("  " + set1[i]);
					i++;
				}
				else if (set1[i] > set2[j])
				{
					System.out.print("  " + set2[j]);
					j++;
				}
			}
			else if (i < n)
			{
				if (is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					{
						//When element are not exist in set2
						System.out.print("  " + set1[i]);
					}
				}
				i++;
			}
			else
			{
				if (is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					{
						//When element are not exist in set1
						System.out.print("  " + set2[j] + "");
					}
				}
				j++;
			}
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		FindUnions obj = new FindUnions();
		//Define sorted elements sets
		int[] set1 = {
			1,
			2,
			2,
			5,
			6,
			6,
			8
		};
		int[] set2 = {
			1,
			2,
			2,
			2,
			3,
			3,
			6,
			6
		};
		//Get the size
		int n = set1.length;
		int m = set2.length;
		// 1 2 3 5 6 8
		obj.find_union(set1, set2, n, m);
		int[] set3 = {
			2,
			7,
			8,
			8,
			9
		};
		int[] set4 = {
			3,
			5,
			6,
			7,
			7,
			7
		};
		//Get the size
		n = set3.length;
		m = set4.length;
		// 2 3 5 6 7 8 9
		obj.find_union(set3, set4, n, m);
		int[] set5 = {
			1,
			2
		};
		int[] set6 = {
			8,
			9,
			11
		};
		//Get the size
		n = set5.length;
		m = set6.length;
		obj.find_union(set5, set6, n, m);
	}
}

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
//Include header file
#include <iostream>

using namespace std;
// C++ program
// Find the union of two sorted arrays
class FindUnions
{
	public:
		//Display elements of given record
		void display(int record[], int size)
		{
			for (int i = 0; i < size; i++)
			{
				cout << "  " << record[i];
			}
		}
	//Check that whether location element already exists in given record
	bool is_already_exist(int record[], int location)
	{
		if (location > 0 && record[location] == record[location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	void find_union(int set1[], int set2[], int n, int m)
	{
		//Loop controlling variables
		int i = 0;
		int j = 0;
		cout << "\n Set A : ";
		this->display(set1, n);
		cout << "\n Set B : ";
		this->display(set2, m);
		cout << "\n Union : ";
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (this->is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i++;
				}
				else if (this->is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j++;
				}
				else if (j > 0 && set1[i] == set2[j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					i++;
				}
				else if (i > 0 && set2[j] == set1[i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					j++;
				}
				else if (set1[i] == set2[j])
				{
					//when get a new common unique elements
					cout << "  " << set1[i];
					i++;
					j++;
				}
				else if (set1[i] < set2[j])
				{
					cout << "  " << set1[i];
					i++;
				}
				else if (set1[i] > set2[j])
				{
					cout << "  " << set2[j];
					j++;
				}
			}
			else if (i < n)
			{
				if (this->is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					{
						//When element are not exist in set2
						cout << "  " << set1[i];
					}
				}
				i++;
			}
			else
			{
				if (this->is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					{
						//When element are not exist in set1
						cout << "  " << set2[j] << "";
					}
				}
				j++;
			}
		}
		cout << "\n";
	}
};
int main()
{
	FindUnions obj = FindUnions();
	//Define sorted elements sets
	int set1[] = {
		1 , 2 , 2 , 5 , 6 , 6 , 8
	};
	int set2[] = {
		1 , 2 , 2 , 2 , 3 , 3 , 6 , 6
	};
	//Get the size
	int n = sizeof(set1) / sizeof(set1[0]);
	int m = sizeof(set2) / sizeof(set2[0]);
	// 1 2 3 5 6 8
	obj.find_union(set1, set2, n, m);
	int set3[] = {
		2 , 7 , 8 , 8 , 9
	};
	int set4[] = {
		3 , 5 , 6 , 7 , 7 , 7
	};
	//Get the size
	n = sizeof(set3) / sizeof(set3[0]);
	m = sizeof(set4) / sizeof(set4[0]);
	// 2 3 5 6 7 8 9
	obj.find_union(set3, set4, n, m);
	int set5[] = {
		1 , 2
	};
	int set6[] = {
		8 , 9 , 11
	};
	//Get the size
	n = sizeof(set5) / sizeof(set5[0]);
	m = sizeof(set6) / sizeof(set6[0]);
	obj.find_union(set5, set6, n, m);
	return 0;
}

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
//Include namespace system
using System;

// C# program
// Find the union of two sorted arrays

class FindUnions
{
	//Display elements of given record
	public void display(int[] record, int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write("  " + record[i]);
		}
	}
	//Check that whether location element already exists in given record
	public Boolean is_already_exist(int[] record, int location)
	{
		if (location > 0 && record[location] == record[location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	public void find_union(int[] set1, int[] set2, int n, int m)
	{
		//Loop controlling variables
		int i = 0;
		int j = 0;
		Console.Write("\n Set A : ");
		display(set1, n);
		Console.Write("\n Set B : ");
		display(set2, m);
		Console.Write("\n Union : ");
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i++;
				}
				else if (is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j++;
				}
				else if (j > 0 && set1[i] == set2[j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					i++;
				}
				else if (i > 0 && set2[j] == set1[i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					j++;
				}
				else if (set1[i] == set2[j])
				{
					//when get a new common unique elements
					Console.Write("  " + set1[i]);
					i++;
					j++;
				}
				else if (set1[i] < set2[j])
				{
					Console.Write("  " + set1[i]);
					i++;
				}
				else if (set1[i] > set2[j])
				{
					Console.Write("  " + set2[j]);
					j++;
				}
			}
			else if (i < n)
			{
				if (is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					{
						//When element are not exist in set2
						Console.Write("  " + set1[i]);
					}
				}
				i++;
			}
			else
			{
				if (is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					{
						//When element are not exist in set1
						Console.Write("  " + set2[j] + "");
					}
				}
				j++;
			}
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		FindUnions obj = new FindUnions();
		//Define sorted elements sets
		int[] set1 = {
			1 , 2 , 2 , 5 , 6 , 6 , 8
		};
		int[] set2 = {
			1 , 2 , 2 , 2 , 3 , 3 , 6 , 6
		};
		//Get the size
		int n = set1.Length;
		int m = set2.Length;
		// 1 2 3 5 6 8
		obj.find_union(set1, set2, n, m);
		int[] set3 = {
			2 , 7 , 8 , 8 , 9
		};
		int[] set4 = {
			3 , 5 , 6 , 7 , 7 , 7
		};
		//Get the size
		n = set3.Length;
		m = set4.Length;
		// 2 3 5 6 7 8 9
		obj.find_union(set3, set4, n, m);
		int[] set5 = {
			1 , 2
		};
		int[] set6 = {
			8 , 9 , 11
		};
		//Get the size
		n = set5.Length;
		m = set6.Length;
		obj.find_union(set5, set6, n, m);
	}
}

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
<?php
// Php program
// Find the union of two sorted arrays
class FindUnions
{
	//Display elements of given record
	public	function display( & $record, $size)
	{
		for ($i = 0; $i < $size; $i++)
		{
			echo "  ". $record[$i];
		}
	}
	//Check that whether location element already exists in given record
	public	function is_already_exist( & $record, $location)
	{
		if ($location > 0 && $record[$location] == $record[$location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	public	function find_union( & $set1, & $set2, $n, $m)
	{
		//Loop controlling variables
		$i = 0;
		$j = 0;
		echo "\n Set A : ";
		$this->display($set1, $n);
		echo "\n Set B : ";
		$this->display($set2, $m);
		echo "\n Union : ";
		while ($i < $n || $j < $m)
		{
			//Check that i and j location are valid to given sets or not
			if ($i < $n && $j < $m)
			{
				//When [i] and [j] location element exist in given sets 
				if ($this->is_already_exist($set1, $i))
				{
					//when set-1 [i] location element are already exist
					$i++;
				}
				else if ($this->is_already_exist($set2, $j))
				{
					//when set-2 [j] location element are already exist
					$j++;
				}
				else if ($j > 0 && $set1[$i] == $set2[$j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					$i++;
				}
				else if ($i > 0 && $set2[$j] == $set1[$i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					$j++;
				}
				else if ($set1[$i] == $set2[$j])
				{
					//when get a new common unique elements
					echo "  ". $set1[$i];
					$i++;
					$j++;
				}
				else if ($set1[$i] < $set2[$j])
				{
					echo "  ". $set1[$i];
					$i++;
				}
				else if ($set1[$i] > $set2[$j])
				{
					echo "  ". $set2[$j];
					$j++;
				}
			}
			else if ($i < $n)
			{
				if ($this->is_already_exist($set1, $i) == false)
				{
					//Check that current element exist in other set
					if ($j == 0 || ($j > 0 && $set1[$i] != $set2[$j - 1]))
					{
						//When element are not exist in set2
						echo "  ". $set1[$i];
					}
				}
				$i++;
			}
			else
			{
				if ($this->is_already_exist($set2, $j) == false)
				{
					//Check that current element exist in other set
					if ($i == 0 || ($i > 0 && $set2[$j] != $set1[$i - 1]))
					{
						//When element are not exist in set1
						echo "  ". $set2[$j] ."";
					}
				}
				$j++;
			}
		}
		echo "\n";
	}
}

function main()
{
	$obj = new FindUnions();
	//Define sorted elements sets
	$set1 = array(1, 2, 2, 5, 6, 6, 8);
	$set2 = array(1, 2, 2, 2, 3, 3, 6, 6);
	//Get the size
	$n = count($set1);
	$m = count($set2);
	// 1 2 3 5 6 8
	$obj->find_union($set1, $set2, $n, $m);
	$set3 = array(2, 7, 8, 8, 9);
	$set4 = array(3, 5, 6, 7, 7, 7);
	//Get the size
	$n = count($set3);
	$m = count($set4);
	// 2 3 5 6 7 8 9
	$obj->find_union($set3, $set4, $n, $m);
	$set5 = array(1, 2);
	$set6 = array(8, 9, 11);
	//Get the size
	$n = count($set5);
	$m = count($set6);
	$obj->find_union($set5, $set6, $n, $m);
}
main();

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
// Node Js program
// Find the union of two sorted arrays
class FindUnions
{
	//Display elements of given record
	display(record, size)
	{
		for (var i = 0; i < size; i++)
		{
			process.stdout.write("  " + record[i]);
		}
	}
	//Check that whether location element already exists in given record
	is_already_exist(record, location)
	{
		if (location > 0 && record[location] == record[location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	find_union(set1, set2, n, m)
	{
		//Loop controlling variables
		var i = 0;
		var j = 0;
		process.stdout.write("\n Set A : ");
		this.display(set1, n);
		process.stdout.write("\n Set B : ");
		this.display(set2, m);
		process.stdout.write("\n Union : ");
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (this.is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i++;
				}
				else if (this.is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j++;
				}
				else if (j > 0 && set1[i] == set2[j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					i++;
				}
				else if (i > 0 && set2[j] == set1[i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					j++;
				}
				else if (set1[i] == set2[j])
				{
					//when get a new common unique elements
					process.stdout.write("  " + set1[i]);
					i++;
					j++;
				}
				else if (set1[i] < set2[j])
				{
					process.stdout.write("  " + set1[i]);
					i++;
				}
				else if (set1[i] > set2[j])
				{
					process.stdout.write("  " + set2[j]);
					j++;
				}
			}
			else if (i < n)
			{
				if (this.is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					{
						//When element are not exist in set2
						process.stdout.write("  " + set1[i]);
					}
				}
				i++;
			}
			else
			{
				if (this.is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					{
						//When element are not exist in set1
						process.stdout.write("  " + set2[j] + "");
					}
				}
				j++;
			}
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var obj = new FindUnions();
	//Define sorted elements sets
	var set1 = [1, 2, 2, 5, 6, 6, 8];
	var set2 = [1, 2, 2, 2, 3, 3, 6, 6];
	//Get the size
	var n = set1.length;
	var m = set2.length;
	// 1 2 3 5 6 8
	obj.find_union(set1, set2, n, m);
	var set3 = [2, 7, 8, 8, 9];
	var set4 = [3, 5, 6, 7, 7, 7];
	//Get the size
	n = set3.length;
	m = set4.length;
	// 2 3 5 6 7 8 9
	obj.find_union(set3, set4, n, m);
	var set5 = [1, 2];
	var set6 = [8, 9, 11];
	//Get the size
	n = set5.length;
	m = set6.length;
	obj.find_union(set5, set6, n, m);
}
main();

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
#  Python 3 program
#  Find the union of two sorted arrays
class FindUnions :
	# Display elements of given record
	def display(self, record, size) :
		i = 0
		while (i < size) :
			print("  ", record[i], end = "")
			i += 1
		
	
	# Check that whether location element already exists in given record
	def is_already_exist(self, record, location) :
		if (location > 0 and record[location] == record[location - 1]) :
			return True
		
		return False
	
	# Find the union of given two sorted order sets (array or list)
	def find_union(self, set1, set2, n, m) :
		# Loop controlling variables
		i = 0
		j = 0
		print("\n Set A : ", end = "")
		self.display(set1, n)
		print("\n Set B : ", end = "")
		self.display(set2, m)
		print("\n Union : ", end = "")
		while (i < n or j < m) :
			# Check that i and j location are valid to given sets or not
			if (i < n and j < m) :
				# When [i] and [j] location element exist in given sets 
				if (self.is_already_exist(set1, i)) :
					# when set-1 [i] location element are already exist
					i += 1
				
				elif(self.is_already_exist(set2, j)) :
					# when set-2 [j] location element are already exist
					j += 1
				
				elif(j > 0 and set1[i] == set2[j - 1]) :
					# when set-1 [i] location element are already exist in set 2
					i += 1
				
				elif(i > 0 and set2[j] == set1[i - 1]) :
					# when set-2 [j] location element are already exist in set 1
					j += 1
				
				elif(set1[i] == set2[j]) :
					# when get a new common unique elements
					print("  ", set1[i], end = "")
					i += 1
					j += 1
				
				elif(set1[i] < set2[j]) :
					print("  ", set1[i], end = "")
					i += 1
				
				elif(set1[i] > set2[j]) :
					print("  ", set2[j], end = "")
					j += 1
				
			
			elif(i < n) :
				if (self.is_already_exist(set1, i) == False) :
					# Check that current element exist in other set
					if (j == 0 or(j > 0 and set1[i] != set2[j - 1])) :
						# When element are not exist in set2
						print("  ", set1[i], end = "")
					
				
				i += 1
			else :
				if (self.is_already_exist(set2, j) == False) :
					# Check that current element exist in other set
					if (i == 0 or(i > 0 and set2[j] != set1[i - 1])) :
						# When element are not exist in set1
						print("  ", set2[j] ,"", end = "")
					
				
				j += 1
			
		
		print("\n", end = "")
	

def main() :
	obj = FindUnions()
	# Define sorted elements sets
	set1 = [1, 2, 2, 5, 6, 6, 8]
	set2 = [1, 2, 2, 2, 3, 3, 6, 6]
	# Get the size
	n = len(set1)
	m = len(set2)
	#  1 2 3 5 6 8
	obj.find_union(set1, set2, n, m)
	set3 = [2, 7, 8, 8, 9]
	set4 = [3, 5, 6, 7, 7, 7]
	# Get the size
	n = len(set3)
	m = len(set4)
	#  2 3 5 6 7 8 9
	obj.find_union(set3, set4, n, m)
	set5 = [1, 2]
	set6 = [8, 9, 11]
	# Get the size
	n = len(set5)
	m = len(set6)
	obj.find_union(set5, set6, n, m)

if __name__ == "__main__": main()

Output

 Set A :    1   2   2   5   6   6   8
 Set B :    1   2   2   2   3   3   6   6
 Union :    1   2   3   5   6   8

 Set A :    2   7   8   8   9
 Set B :    3   5   6   7   7   7
 Union :    2   3   5   6   7   8   9

 Set A :    1   2
 Set B :    8   9   11
 Union :    1   2   8    9    11
#  Ruby program
#  Find the union of two sorted arrays
class FindUnions

	# Display elements of given record
	def display(record, size)
	
		i = 0
		while (i < size)
		
			print("  ", record[i])
			i += 1
		end
	end
	# Check that whether location element already exists in given record
	def is_already_exist(record, location)
	
		if (location > 0 && record[location] == record[location - 1])
		
			return true
		end
		return false
	end
	# Find the union of given two sorted order sets (array or list)
	def find_union(set1, set2, n, m)
	
		# Loop controlling variables
		i = 0
		j = 0
		print("\n Set A : ")
		self.display(set1, n)
		print("\n Set B : ")
		self.display(set2, m)
		print("\n Union : ")
		while (i < n || j < m)
		
			# Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			
				# When [i] and [j] location element exist in given sets 
				if (self.is_already_exist(set1, i))
				
					# when set-1 [i] location element are already exist
					i += 1
				elsif(self.is_already_exist(set2, j))
				
					# when set-2 [j] location element are already exist
					j += 1
				elsif(j > 0 && set1[i] == set2[j - 1])
				
					# when set-1 [i] location element are already exist in set 2
					i += 1
				elsif(i > 0 && set2[j] == set1[i - 1])
				
					# when set-2 [j] location element are already exist in set 1
					j += 1
				elsif(set1[i] == set2[j])
				
					# when get a new common unique elements
					print("  ", set1[i])
					i += 1
					j += 1
				elsif(set1[i] < set2[j])
				
					print("  ", set1[i])
					i += 1
				elsif(set1[i] > set2[j])
				
					print("  ", set2[j])
					j += 1
				end
			elsif(i < n)
			
				if (self.is_already_exist(set1, i) == false)
				
					# Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					
						# When element are not exist in set2
						print("  ", set1[i])
					end
				end
				i += 1
			else
			
				if (self.is_already_exist(set2, j) == false)
				
					# Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					
						# When element are not exist in set1
						print("  ", set2[j] ,"")
					end
				end
				j += 1
			end
		end
		print("\n")
	end
end
def main()

	obj = FindUnions.new()
	# Define sorted elements sets
	set1 = [1, 2, 2, 5, 6, 6, 8]
	set2 = [1, 2, 2, 2, 3, 3, 6, 6]
	# Get the size
	n = set1.length
	m = set2.length
	#  1 2 3 5 6 8
	obj.find_union(set1, set2, n, m)
	set3 = [2, 7, 8, 8, 9]
	set4 = [3, 5, 6, 7, 7, 7]
	# Get the size
	n = set3.length
	m = set4.length
	#  2 3 5 6 7 8 9
	obj.find_union(set3, set4, n, m)
	set5 = [1, 2]
	set6 = [8, 9, 11]
	# Get the size
	n = set5.length
	m = set6.length
	obj.find_union(set5, set6, n, m)
end
main()

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
// Scala program
// Find the union of two sorted arrays
class FindUnions
{
	//Display elements of given record
	def display(record: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + record(i));
			i += 1;
		}
	}
	//Check that whether location element already exists in given record
	def is_already_exist(record: Array[Int], location: Int): Boolean = {
		if (location > 0 && record(location) == record(location - 1))
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	def find_union(set1: Array[Int], set2: Array[Int], n: Int, m: Int): Unit = {
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		print("\n Set A : ");
		display(set1, n);
		print("\n Set B : ");
		display(set2, m);
		print("\n Union : ");
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i += 1;
				}
				else if (is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j += 1;
				}
				else if (j > 0 && set1(i) == set2(j - 1))
				{
					//when set-1 [i] location element are already exist in set 2
					i += 1;
				}
				else if (i > 0 && set2(j) == set1(i - 1))
				{
					//when set-2 [j] location element are already exist in set 1
					j += 1;
				}
				else if (set1(i) == set2(j))
				{
					//when get a new common unique elements
					print("  " + set1(i));
					i += 1;
					j += 1;
				}
				else if (set1(i) < set2(j))
				{
					print("  " + set1(i));
					i += 1;
				}
				else if (set1(i) > set2(j))
				{
					print("  " + set2(j));
					j += 1;
				}
			}
			else if (i < n)
			{
				if (is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1(i) != set2(j - 1)))
					{
						//When element are not exist in set2
						print("  " + set1(i));
					}
				}
				i += 1;
			}
			else
			{
				if (is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2(j) != set1(i - 1)))
					{
						//When element are not exist in set1
						print("  " + set2(j) + "");
					}
				}
				j += 1;
			}
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: FindUnions = new FindUnions();
		//Define sorted elements sets
		var set1: Array[Int] = Array(1, 2, 2, 5, 6, 6, 8);
		var set2: Array[Int] = Array(1, 2, 2, 2, 3, 3, 6, 6);
		//Get the size
		var n: Int = set1.length;
		var m: Int = set2.length;
		// 1 2 3 5 6 8
		obj.find_union(set1, set2, n, m);
		var set3: Array[Int] = Array(2, 7, 8, 8, 9);
		var set4: Array[Int] = Array(3, 5, 6, 7, 7, 7);
		//Get the size
		n = set3.length;
		m = set4.length;
		// 2 3 5 6 7 8 9
		obj.find_union(set3, set4, n, m);
		var set5: Array[Int] = Array(1, 2);
		var set6: Array[Int] = Array(8, 9, 11);
		//Get the size
		n = set5.length;
		m = set6.length;
		obj.find_union(set5, set6, n, m);
	}
}

Output

 Set A :   1  2  2  5  6  6  8
 Set B :   1  2  2  2  3  3  6  6
 Union :   1  2  3  5  6  8

 Set A :   2  7  8  8  9
 Set B :   3  5  6  7  7  7
 Union :   2  3  5  6  7  8  9

 Set A :   1  2
 Set B :   8  9  11
 Union :   1  2  8  9  11
// Swift program
// Find the union of two sorted arrays
class FindUnions
{
	//Display elements of given record
	func display(_ record: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", record[i], terminator: "");
			i += 1;
		}
	}
	//Check that whether location element already exists in given record
	func is_already_exist(_ record: [Int], _ location: Int) -> Bool
	{
		if (location > 0 && record[location] == record[location - 1])
		{
			return true;
		}
		return false;
	}
	//Find the union of given two sorted order sets (array or list)
	func find_union(_ set1: [Int], _ set2: [Int], _ n: Int, _ m: Int)
	{
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		print("\n Set A : ", terminator: "");
		self.display(set1, n);
		print("\n Set B : ", terminator: "");
		self.display(set2, m);
		print("\n Union : ", terminator: "");
		while (i < n || j < m)
		{
			//Check that i and j location are valid to given sets or not
			if (i < n && j < m)
			{
				//When [i] and [j] location element exist in given sets 
				if (self.is_already_exist(set1, i))
				{
					//when set-1 [i] location element are already exist
					i += 1;
				}
				else if (self.is_already_exist(set2, j))
				{
					//when set-2 [j] location element are already exist
					j += 1;
				}
				else if (j > 0 && set1[i] == set2[j - 1])
				{
					//when set-1 [i] location element are already exist in set 2
					i += 1;
				}
				else if (i > 0 && set2[j] == set1[i - 1])
				{
					//when set-2 [j] location element are already exist in set 1
					j += 1;
				}
				else if (set1[i] == set2[j])
				{
					//when get a new common unique elements
					print("  ", set1[i], terminator: "");
					i += 1;
					j += 1;
				}
				else if (set1[i] < set2[j])
				{
					print("  ", set1[i], terminator: "");
					i += 1;
				}
				else if (set1[i] > set2[j])
				{
					print("  ", set2[j], terminator: "");
					j += 1;
				}
			}
			else if (i < n)
			{
				if (self.is_already_exist(set1, i) == false)
				{
					//Check that current element exist in other set
					if (j == 0 || (j > 0 && set1[i] != set2[j - 1]))
					{
						//When element are not exist in set2
						print("  ", set1[i], terminator: "");
					}
				}
				i += 1;
			}
			else
			{
				if (self.is_already_exist(set2, j) == false)
				{
					//Check that current element exist in other set
					if (i == 0 || (i > 0 && set2[j] != set1[i - 1]))
					{
						//When element are not exist in set1
						print("  ", set2[j] ,"", terminator: "");
					}
				}
				j += 1;
			}
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: FindUnions = FindUnions();
	//Define sorted elements sets
	let set1: [Int] = [1, 2, 2, 5, 6, 6, 8];
	let set2: [Int] = [1, 2, 2, 2, 3, 3, 6, 6];
	//Get the size
	var n: Int = set1.count;
	var m: Int = set2.count;
	// 1 2 3 5 6 8
	obj.find_union(set1, set2, n, m);
	let set3: [Int] = [2, 7, 8, 8, 9];
	let set4: [Int] = [3, 5, 6, 7, 7, 7];
	//Get the size
	n = set3.count;
	m = set4.count;
	// 2 3 5 6 7 8 9
	obj.find_union(set3, set4, n, m);
	let set5: [Int] = [1, 2];
	let set6: [Int] = [8, 9, 11];
	//Get the size
	n = set5.count;
	m = set6.count;
	obj.find_union(set5, set6, n, m);
}
main();

Output

 Set A :    1   2   2   5   6   6   8
 Set B :    1   2   2   2   3   3   6   6
 Union :    1   2   3   5   6   8

 Set A :    2   7   8   8   9
 Set B :    3   5   6   7   7   7
 Union :    2   3   5   6   7   8   9

 Set A :    1   2
 Set B :    8   9   11
 Union :    1   2   8    9    11




Comment

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