JavaScript Program to Find Common Elements Between Two Sorted Arrays using Binary Search Last Updated : 04 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Given two sorted arrays, our task is to find common elements in two sorted arrays then the program should return an array that contains all the elements that are common to the two arrays. The arrays can contain duplicate elements, that is, values at one index in an array can be equal to the values at another index in the array. Example: Input: arr1 = [1, 1, 2, 2, 3, 3];arr2 = [1, 1, 1, 2, 2, 4];Output: [1, 2]ApproachThe "BSearch" function performs a binary search on a sorted array to find the element "x". It iteratively checks the middle element and adjusts the search range until it sees "x" or determines that "x" is not present.The "BSearch" helper function is utilized to efficiently check for the presence of an element in an array, reducing the search time to logarithmic complexity.In "findCommonEleBSearch", the arrays are swapped if necessary to ensure "arr1" is the smaller array. This reduces the number of binary searches needed, enhancing efficiency.The function iterates through each element of "arr1", using "BSearch" to check if the element is in "arr2". If found and not a duplicate of the last added element, it adds the element to "commonelements".The provided arrays "arr1" and "arr2" are compared, and the common elements [1, 4, 9] are correctly identified and printed using the "findCommonEleBSearch" function.Example: The example below shows how to Find common elements in two sorted arrays using a Binary Search Algorithm. JavaScript function BSearch(arr, x) { let low = 0, high = arr.length - 1; while (low <= high) { let mid = Math.floor((low + high) / 2); if (arr[mid] === x) return true; else if (arr[mid] < x) low = mid + 1; else high = mid - 1; } return false; } // Function to find common elements using binary search function findCommonEleBSearch(arr1, arr2) { // Ensure arr1 is the smaller array for efficiency if (arr1.length > arr2.length) { [arr1, arr2] = [arr2, arr1]; } let commonElements = []; let lastAddedElement = null; for (let i = 0; i < arr1.length; i++) { if (arr1[i] !== lastAddedElement && BSearch(arr2, arr1[i])) { commonElements.push(arr1[i]); lastAddedElement = arr1[i]; } } return commonElements; } let arr1 = [1, 3, 4, 6, 7, 9]; let arr2 = [1, 2, 4, 5, 9, 10]; let commonBSearch = findCommonEleBSearch(arr1, arr2); console.log(commonBSearch); Output[ 1, 4, 9 ] Time Complexity: O(m * log n), m is the length of the smaller array and n is the length of the larger array. Space Complexity: O(1). Comment More infoAdvertise with us Next Article JavaScript Program to Find Common Elements Between Two Sorted Arrays using Binary Search Anonymous Improve Article Tags : JavaScript Web Technologies JavaScript-DSA JavaScript-Program Similar Reads JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= 3 min read 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 PHP 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 p 3 min read Find Kth Element of Two Sorted Arrays in JavaScript Given two sorted arrays, our task is to find the Kth element in the combined array made by merging the two input arrays in JavaScript.Example:Input: Arr1: [1, 2, 5] , Arr2:[2, 4, 6, 8], K = 4Output: Kth element is 4.Explanation:The final Array would be: [1, 2, 2, 4, 5, 6, 8]The 4th element of this a 3 min read C Program to Find Common Array Elements Here, we will see how to find the common array elements using a C program. Input: a[6] = {1,2,3,4,5,6} b[6] = {5,6,7,8,9,10} Output: common array elements is 5 6 C // C program to find the common array elements #include <stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int b[6] = { 5, 6, 1 min read How to Find Common Elements in Two Arrays in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the common elements in two arrays in C++. Examples: Input:Arr1: {1, 2, 3, 4, 5}Arr2: {3, 4, 5, 6, 7}Output:Common Elements: 3 4 4 min read Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar 3 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 Time and Space Complexity Analysis of Binary Search Algorithm Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. Example of Binary Search AlgorithmAspectComplexityTime ComplexityO(log n)Space ComplexityO(1) 3 min read PHP Program to 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