Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Find the Lost Element from a Duplicated Array
To find the lost element from a duplicated array in JavaScript, we will be discussing various approaches. Prerequisites to solve this problem include understanding of JavaScript arrays, loops, Set object, and binary search.
In this article we have two arrays where one array is a duplicate of the other with one missing element. Our task is to find the lost element from the duplicated array in JavaScript.
Problem Statement
Given two arrays where the second array is missing exactly one element from the first array, we need to identify that missing element.
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 are four different approaches to find the lost element from a duplicated array in JavaScript:
Using for Loop
This approach uses nested loops where the inner loop iterates over the second array to compare each element with the element selected by the outer loop from the first array.
Algorithm
- Iterate through each element in the first array using an outer loop
- For each element, search for it in the second array using an inner loop
- If an element from the first array is not found in the second array, it's the missing element
- Return the missing element
Example
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; i < arr1.length; i++) {
let found = false;
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
found = true;
break;
}
}
if (!found) {
missingElement = arr1[i];
break;
}
}
return missingElement;
}
console.log("Missing Element: " + missingEle(arr1, arr2));
Missing Element: 4
Using reduce() Function
This approach uses the reduce() function to calculate the sum of elements in both arrays. The difference between the sums gives us the missing element.
Algorithm
- Calculate the sum of all elements in the first array using reduce()
- Calculate the sum of all elements in the second array using reduce()
- Subtract the second sum from the first sum to get the missing element
- Return the missing element
Example
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));
Missing Element: 4
Using Set Object
This approach uses a Set object to efficiently check for element existence. It creates a Set from the second array and checks if each element from the first array exists in the Set.
Algorithm
- Create a Set object from the second array for O(1) lookup time
- Iterate through elements of the first array using for...of loop
- Use the has() method to check if each element exists in the Set
- If an element is not found in the Set, it's the missing element
Example
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));
Missing Element: 4
Using Binary Search
This approach uses binary search to find the missing element efficiently. It requires both arrays to be sorted and works by comparing elements at the middle positions.
Algorithm
- Set left and right pointers to define the search range
- Calculate the middle index and compare elements at that position in both arrays
- If elements are equal, search in the right half; otherwise, search in the left half
- Continue until the missing element is found
Example
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 (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr1[mid] === arr2[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return arr1[left];
}
console.log("Missing Element: " + missingEle(arr1, arr2));
Missing Element: 4
Complexity Comparison
Here is a comparison of time and space complexity of all the 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
We explored four different approaches to find the missing element from a duplicated array. Binary search offers the best time complexity of O(log n) for sorted arrays, while the reduce() function and Set object provide good performance for unsorted arrays. Choose the approach based on whether your arrays are sorted and your specific performance requirements.
