- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript program to find the lost element from a duplicated array
We have problem to write a program in JavaScript that can identify the missing element in a duplicated array. The input for the program will be an array that contains duplicate elements except for one, which is missing. The program should be able to identify the missing element and return it as output.
Prerequisite to solve this problem requires an understanding of JavaScript arrays and how to manipulate them to find the missing element. We must develop a program that should be efficient and able to handle large arrays with many duplicate elements.
Problem Statement
We are given two arrays that are duplicates of each other except one element, that is one element from one of the arrays is missing, we need to find that missing element.
Example
Given the following two arrays −
Input
[1, 2, 3, 4, 5, 6] [1, 3, 4, 5, 6]
Output
2
The missing element from the second array is 2. We need to find this missing element.
Example
Given the following two arrays −
Input
['a', 'b', 'c', 'd', 'e'] ['a', 'c', 'd', 'e']
Output
b
The missing element from the second array is b. We need to find this missing element.
Now we will look at some of the methods for solving the above problem.
Method 1: Using for Loops
In this method, we will iterate over the elements in the first array and check if they exist in the second array. If an element does not exist in the second array, it is our missing element.
Algorithm
Initialize a variable to hold the missing element.
Loop through the first array.
For each element in the first array, check if it exists in the second array using a nested loop.
If the element does not exist in the second array, assign it to the missing element variable and break out of the nested loop.
Return the missing element.
Example
<!DOCTYPE html> <html> <body> <h2>Find the Missing Element in a Duplicated Array</h2> <div id="arrays"></div> <br> <div id="output"></div> <script> function findMissingElementUsingForLoop(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; } const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [1, 3, 4, 5, 6]; const arraysDiv = document.getElementById("arrays"); arraysDiv.innerHTML = `Array 1: ${arr1.join(", ")}<br>Array 2: ${arr2.join(", ")}`; const missingElement = findMissingElementUsingForLoop(arr1, arr2); document.getElementById("output").innerHTML = `The missing element is: ${missingElement}`; </script> </body> </html>
Method 2: Using Filter and IndexOf Functions
In this method, we will use the filter function to find the element that is not present in both arrays and then we will use the indexOf function to return that element.
Algorithm
Define the function findMissingElementUsingFilterAndIndexOf that takes two arrays as input.
Use the filter function to create a new array containing all elements in arr1 that are not present in arr2.
Use the indexOf function to return the first element in the new array.
Example
<!DOCTYPE html> <html> <body> <h2>Find the Missing Element in a Duplicated Array</h2> <div id="arrays"></div> <br> <div id="output"></div> <script> function findMissingElementUsingFilterAndIndexOf(arr1, arr2) { const diffArr = arr1.filter(element => !arr2.includes(element)); return diffArr[0]; } const arr1 = [11, 22, 33, 44, 55, 66]; const arr2 = [11, 33, 44, 55, 66]; const arraysDiv = document.getElementById("arrays"); arraysDiv.innerHTML = `Array 1: ${arr1.join(", ")}<br>Array 2: ${arr2.join(", ")}`; const missingElement = findMissingElementUsingFilterAndIndexOf(arr1, arr2); document.getElementById("output").innerHTML = `The missing element is: ${missingElement}`; </script> </body> </html>
Method 3: Using Reduce Function
In this method, we will use the reduce function to find the sum of all elements in both arrays. Then we will subtract the sum of the second array from the sum of the first array to get the missing element.
Algorithm
Define a function findMissingElementUsingReduce that takes two arrays as input.
Use the reduce function on the first array to find the sum of all elements and assign it to a variable sum1.
Use the reduce function on the second array to find the sum of all elements and assign it to a variable sum2.
Subtract sum2 from sum1 to get the missing element.
Return the missing element.
Example
<!DOCTYPE html> <html> <body> <h2>Find the Missing Element in a Duplicated Array</h2> <div id="arrays"></div> <br> <div id="output"></div> <script> function findMissingElementUsingReduce(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; } const arr1 = [121, 122, 123, 124, 125, 126]; const arr2 = [121, 123, 124, 125, 126]; const arraysDiv = document.getElementById("arrays"); arraysDiv.innerHTML = `Array 1: ${arr1.join(", ")}<br>Array 2: ${arr2.join(", ")}`; const missingElement = findMissingElementUsingReduce(arr1, arr2); document.getElementById("output").innerHTML = `The missing element is: ${missingElement}`; </script> </body> </html>
Method 4: Using Set Object
This is our last and final method to come up with a code that can find the missing element from a duplicated array. Here, we can create a new Set object from one of the arrays and then check if each element in the other array exists in the Set. If an element does not exist in the Set, then it must be the missing element.
Algorithm
Create a new Set object from one of the arrays.
Loop through the other array and check if each element exists in the Set.
If an element does not exist in the Set, then it is the missing element.
Return the missing element.
Example
<!DOCTYPE html> <html> <body> <h2>Find the Missing Element in a Duplicated Array</h2> <div id="arrays"></div> <br> <div id="output"></div> <script> function findMissingElementUsingSet(arr1, arr2) { const set = new Set(arr2); for (let element of arr1) { if (!set.has(element)) { return element; } } return null; } const arr1 = [134, 234, 334, 434, 534, 634]; const arr2 = [134, 334, 434, 534, 634]; const arraysDiv = document.getElementById("arrays"); arraysDiv.innerHTML = `Array 1: ${arr1.join(", ")}<br>Array 2: ${arr2.join(", ")}`; const missingElement = findMissingElementUsingSet(arr1, arr2); const outputDiv = document.getElementById("output"); if (missingElement !== null) { outputDiv.innerHTML = `The missing element is: ${missingElement}`; } else { outputDiv.innerHTML = "There is no missing element in the arrays."; } </script> </body> </html>
Conclusion
In this blog we have discussed four methods of finding a lost element from a duplicated array, those are using for loop, using reduce function, using set object, Using Filter and IndexOf Functions.