PHP Program to Print uncommon elements from two sorted arrays Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share 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.Steps to find uncommon elements between two sorted arrays:Initialize Pointers: Set three-pointers, i, j, and k, to start at the beginning of arrays arr1, arr2, and the result set respectively.Compare Array Elements: Compare current elements of arr1 and arr2. Print the smaller element if they differ and move the respective pointer.Skip Common Elements: If elements of both arrays are the same, increase both i and j to skip these common elements.Print Remaining Elements: After exiting the loop, print any remaining elements in arr1 or arr2 if one array is longer than the other.Output the Results: Echo each unique element not shared by both arrays, ensuring only uncommon elements are printed.Below is the implementation of above approach: PHP <?php // PHP program to find uncommon // elements of two sorted arrays function printUncommon($arr1, $arr2, $n1, $n2) { $i = 0; $j = 0; $k = 0; while ($i < $n1 && $j < $n2) { // If not common, print smaller if ($arr1[$i] < $arr2[$j]) { echo $arr1[$i] . " "; $i++; $k++; } else if ($arr2[$j] < $arr1[$i]) { echo $arr2[$j] . " "; $k++; $j++; } // Skip common element else { $i++; $j++; } } // printing remaining elements while ($i < $n1) { echo $arr1[$i] . " "; $i++; $k++; } while ($j < $n2) { echo $arr2[$j] . " "; $j++; $k++; } } // Driver code $arr1 = array(10, 20, 30); $arr2 = array(20, 25, 30, 40, 50); $n1 = sizeof($arr1) ; $n2 = sizeof($arr2) ; printUncommon($arr1, $arr2, $n1, $n2); // This code is contributed by Anuj_67 ?> Output10 25 40 50Complexity 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 Print uncommon elements from two sorted arrays K kartik Follow Improve Article Tags : PHP two-pointer-algorithm Practice Tags : two-pointer-algorithm Similar Reads Javascript Program to 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 50We do not print 20 and 30 as theseelements are pr 2 min read 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 PHP Program for Median of two sorted arrays of same size Write a PHP program for a given 2 sorted arrays A and B of size n each. the task is to find the median of the array obtained by merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). Examples: Input: ar1[] = {1, 12, 15, 26, 38} ar2[] = {2, 13, 17, 30, 45}Output: 16E 6 min read PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr 3 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache 4 min read Like