
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Lost Element from a Duplicated Array in JavaScript
To find the lost element from a duplicated array in JavaScript, we will be discussing various approaches.Prerequisite to solve this problem requires an understanding of JavaScript arrays, loops, set object and binary search.
In this article we are having two arrays where one array is duplicate of other with a missing element, our task is to find the lost element from a duplicated array in JavaScript.
Example
Input: array1 = [1, 2, 3, 4, 5, 6] array2 = [1, 2, 3, 5, 6] Output: Missing element: 4
Approaches to Find the Lost Element
Here is a list of approaches to find the lost element from a duplicated array in JavaScript which we will be discussing in this article with stepwise explanation and complete example codes.
Using for Loop
To find the lost element from a duplicated array in JavaScript, we are using loops where inner loops iterates over second array comparing each element with element selected by outer loop in array1.
- We have declared two arrays, arr1 and arr2 and defined a function missingEle() that accepts arr1 and arr2 as its argument.
- In the function, we have declared a variable missingElement to hold the missing element.
- We have used two for loop to iterate over elements. The outer loop iterates over arr1, one element at a time in each iteration and inner loop iterates and compare every element of arr2 with the each element of outer loop.
- If the missing element is found then found is set to true which was initially set to false and the loop breaks.
- After completion of loop, if found is still false, then that value is stored in missingElement and loop breaks.
- The missingElement is then returned. Call the missingEle() function with the arr1 and arr2 as its argument.
- Print the result to the console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps to find the lost element from a duplicated array in JavaScript using for loop.
const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [1, 2, 3, 5, 6]; function missingEle(arr1, arr2) { let missingElement; for (let i = 0; iUsing reduce() Function
In this approach to find the lost element from a duplicated array in JavaScript we have used reduce() function. Here we have reduce() function to sum all the elements present in the provided array.
- We have declared two arrays, arr1 and arr2 and defined a function missingEle() that accepts arr1 and arr2 as its argument.
- Then we have used reduce() function to iterate over array and store the sum of all the elements of array.
- It accepts acc and curr as its argument, where curr represents the current element in the array and acc stores the cumulative sum which is initially set to 0.
- Using reduce() function we find the sum of both the arrays which is stored in variable sum1 and sum2.
- Then we calculate the missingElement by subtracting sum1 and sum2 and return missingElement.
- Call the missingEle() function with the arr1 and arr2 as its argument and print the result to the console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps to find the lost element from a duplicated array in JavaScript using reduce() function.
const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [1, 2, 3, 5, 6]; function missingEle(arr1, arr2) { const sum1 = arr1.reduce((acc, curr) => acc + curr, 0); const sum2 = arr2.reduce((acc, curr) => acc + curr, 0); const missingElement = sum1 - sum2; return missingElement; } console.log("Missing Element: " +missingEle(arr1, arr2));
Using set() Object
In this approach we have used set() object to find the lost element from a duplicated array in JavaScript. It checks whether a particular element exists in a set using has() function.
- We have declared two arrays, arr1 and arr2 and defined a function missingEle() that accepts arr1 and arr2 as its argument.
- Create a new Set object from arr2. Loop through the arr1 using for...of and check if each element exists in the set using if/else statement and has() method.
- If an element does not exist in the Set, then it is the missing element. Return the missing element.
- Call the missingEle() function with the arr1 and arr2 as its argument and print the result to the console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps to find the lost element from a duplicated array in JavaScript using set() object.
const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [1, 2, 3, 5, 6]; function missingEle(arr1, arr2) { const set = new Set(arr2); for (let element of arr1) { if (!set.has(element)) { return element; } } return null; } console.log("Missing Element: " +missingEle(arr1, arr2));
Using Binary Search
In this approach to find the lost element from a duplicated array in JavaScript we have used binary search.
- We have declared two arrays, arr1 and arr2 and defined a function missingEle() that accepts arr1 and arr2 as its argument.
- Inside the function missingEle, first we have defined left and right pointer to calculate the middle element.
- We have used while loop to iterate over elements till left pointer is less than right pointer.
- If the middle element of arr1 and arr2 are the same, then we will search in the right half and we will set our left pointer to mid + 1. If middle element of both arrays are not equal then we will search in left half of the array and we will set our right pointer to mid - 1.
- If the missing element is still not found till, left then return arr1[left].
- Call the missingEle() function with the arr1 and arr2 as its argument and print the result to the console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps to find the lost element from a duplicated array in JavaScript using binary search.
const arr1 = [1, 2, 3, 4, 5]; const arr2 = [1, 2, 3, 5]; function missingEle(arr1, arr2) { let left = 0, right = arr1.length - 1; while (leftComplexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach Time Complexity Space Complexity for loop O(n*m) O(1) reduce() function O(n+m) O(1) set() object O(n+m) O(m) Binary search O(log n) O(1) Conclusion
In this article to find the lost element from a duplicated array in JavaScript, we have discussed four different approaches. These approaches are: by using for loop, reduce() function, set() object and binary search. Using binary search is more efficient here as it has logrithmic time complexity.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.