

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Make an array of another array's duplicate values in JavaScript
We are required to write a JavaScript function that takes in an array of literals. The function should prepare a new array of all those elements from the original array that are not unique (duplicate elements.)
For example −
If the input array is −
const arr = [3, 6, 7, 5, 3];
Then the output should be −
const output = [3];
Example
The code for this will be −
const arr = [3, 6, 7, 5, 3]; const makeDuplicatesArray = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ continue; }; res.push(arr[i]) }; return res; }; console.log(makeDuplicatesArray(arr));
Output
And the output in the console will be −
[3]
- Related Questions & Answers
- Getting elements of an array depending on corresponding values of another JavaScript
- Sum all duplicate values in array in JavaScript
- Modify an array based on another array JavaScript
- Maximum in an array that can make another array sorted in C++
- Sort an array according to another array in JavaScript
- JavaScript in filter an associative array with another array
- C++ Permutation of an Array that has Smaller Values from Another Array
- Removing duplicate values in a twodimensional array in JavaScript
- Order an array of words based on another array of words JavaScript
- Creating an array of objects based on another array of objects JavaScript
- How to find duplicate values in a JavaScript array?
- How to duplicate elements of an array in the same array with JavaScript?
- Sorting an array of binary values - JavaScript
- Filter JavaScript array of objects with another array
- How to extend an existing JavaScript array with another array?
Advertisements