- Related Questions & Answers
- Find the element that appears once in an array where every other element appears twice in C++
- Find the element that appears once in sorted array - JavaScript
- Finding number that appears for odd times - JavaScript
- Return the index of first character that appears twice in a string in JavaScript
- Add two array keeping duplicates only once - JavaScript
- 8085 program to find the element that appears once
- Finding two golden numbers in JavaScript
- Finding missing element in an array of numbers in JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Is it true that opportunity knocks only once?
- Twice join of two strings in JavaScript
- Finding missing letter in a string - JavaScript
- Elements that appear twice in array in JavaScript
- Finding two numbers given their sum and Highest Common Factor using JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are required to write a JavaScript function that takes in an array in which all the numbers appear thrice except one which appears twice and one which appears only one. Our function should find and return these two numbers.

Following is the code −

const arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => { let x = 0; let y = 0; for(let i = 0; i < arr.length; i++){ if(arr.filter(a => a === arr[i]).length === 2){ y = arr[i]; }; if(arr.filter(b => b === arr[i]).length === 1){ x = arr[i]; }; }; return [x, y]; }; console.log(findMissing(arr));

Following is the console output −

[3, 2]

Advertisements