Javascript Program to Print uncommon elements from two sorted arrays Last Updated : 16 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples :Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are present in both arrays.Input : arr1[] = {10, 20, 30} arr2[] = {40, 50}Output : 10 20 30 40 50 The idea is based on merge process of merge sort. We traverse both arrays and skip common elements.Example: JavaScript // JavaScript program to find uncommon elements // of two sorted arrays function printUncommon(arr1, arr2, n1, n2) { let i = 0, j = 0, k = 0; let output = '' while (i < n1 && j < n2) { // If not common, print smaller if (arr1[i] < arr2[j]) { output += arr1[i] + " "; i++; k++; } else if (arr2[j] < arr1[i]) { output += arr2[j] + " "; k++; j++; } // Skip common element else { i++; j++; } } // printing remaining elements while (i < n1) { output += arr1[i] + " "; i++; k++; } while (j < n2) { output += arr2[j] + " "; j++; k++; } console.log(output); } // Driver Code let arr1 = [10, 20, 30]; let arr2 = [20, 25, 30, 40, 50]; let n1 = arr1.length; let n2 = arr2.length; printUncommon(arr1, arr2, n1, n2); // This code is contributed by susmitakundugoaldanga. Output10 25 40 50 Complexity Analysis:Time Complexity: O(n1 + n2), where n1 and n2 represents the size of the given two arrays.Auxiliary Space: O(1), no extra space is required, so it is a constant.Please refer complete article on Print uncommon elements from two sorted arrays for more details! Comment More infoAdvertise with us Next Article Javascript Program to Print uncommon elements from two sorted arrays K kartik Follow Improve Article Tags : Searching JavaScript Web Technologies DSA Arrays two-pointer-algorithm +2 More Practice Tags : ArraysSearchingtwo-pointer-algorithm Similar Reads Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements ar 6 min read JavaScript - Print Unique Elements From Two Unsorted Arrays Here are the different approaches to print unique elements from two unsorted arrays using JavaScript1. Using filter() methodWe can filter all the unique elements using the filter() method. Then we will make one new array to concat our filtered array.JavaScriptlet a1 = [54, 71, 58, 95, 20]; let a2 = 2 min read JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu 3 min read Union and Intersection of Two Sorted Arrays - Complete Tutorial Union of two arrays is an array having all distinct elements that are present in either array whereas Intersection of two arrays is an array containing distinct common elements between the two arrays. In this post, we will discuss about Union and Intersection of sorted arrays. To know about union an 6 min read Javascript Program for Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] 4 min read Like