Untitled

 avatar
BlueDragon2707
java
2 years ago
842 B
1
Indexable
Never
package sort;

public class UnionOfTwoSortedArray {

	public static void main(String[] args) {

		int arr1[] = { 2, 15, 20, 20, 30 };
		int arr2[] = { 10, 11, 15, 30, 40 };

		int i = 0, j = 0;

		while (i < arr1.length && j < arr2.length) {

			if (i > 0 && arr1[i] == arr1[i - 1]) {
				i++;
				continue;
			}

			if (j > 0 && arr2[j] == arr2[j - 1]) {
				j++;
				continue;
			}

			if (arr1[i] < arr2[j]) {
				System.out.println(arr1[i]);
				i++;
			} else if (arr2[j] < arr1[i]) {
				System.out.println(arr2[j]);
				j++;
			} else {
				System.out.println(arr1[i]);
				i++;
				j++;
			}
		}

		// printing remaining elements

		while (i < arr1.length) {
			System.out.println(arr1[i]);
			i++;
		}

		while (j < arr2.length) {
			System.out.println(arr2[j]);
			j++;
		}

	}

}