How to Remove Duplicates from Array in JavaScript? 5 Ways

How to remove duplicates from an array in JavaScript? Read on to discover the solutions.

We cannot deny the fact that sometimes, we accidentally put the same value twice from an array in JavaScript, but how do we remove it?

Don’t worry because there are special tools in JavaScript that can help us fix this problem and get the result we want.

Well, say goodbye to duplicates as this article shows you the different methods or ways JavaScript removes duplicates from an array.

What is Arrays in JavaScript?

Arrays in JavaScript are ordered collections of values, indexed by numbers.

They are incredibly versatile and can store various data types, including numbers, strings, objects, and even other arrays.

Here’s an of how arrays work in JavaScript:

const subjects = ['Math', 'English', 'Science', 'Programming', 'Sogtware Engineering'];

console.log(subjects[3]); 
console.log(subjects.length);

Arrays are zero-indexed, meaning the first element is at index 0, the second at 1, and so on.

In our given example, we have an array called subjects containing five elements.

Here’s the output:

Programming
5

Now that we’ve got the basics of arrays down, let’s take a closer look at different ways to remove duplicates from array in JavaScript.

Different Ways on How to Remove Duplicates from Array in JavaScript?

Here are some common solutions to remove duplicates from an array in JS:

Solution 1: Use the Set object and the spread operator

One way to remove duplicates from an array in JS is to use the Set object and the spread operator ….

For example:

const subjects = ['Math', 'English', 'Math', 'Programming', 'Software Engineering','English'];
let newArray = [...new Set(subjects)]; ✅
console.log(newArray); 

Output:

[ 'Math', 'English', 'Programming', 'Software Engineering' ]

Solution 2: Use the filter method

Another way to remove duplicates from an array in JS is to use the filter method.

For example:

const subjects = ['Programming', 'English', 'Math', 'Programming', 'Software Engineering','Math'];
let newArray = subjects.filter((item, index) => subjects.indexOf(item) === index); ✅
console.log(newArray); 

Output:

[ 'Programming', 'English', 'Math', 'Software Engineering' ]

Solution 3: Use the reduce method

You can also use the reduce method to remove duplicates from an array.

const subjects = ['Software Engineering', 'Programming', 'Math', 'Programming', 'Software Engineering','Math'];
let newArray = subjects.reduce((accumulator, currentValue) => { ✅
    if (accumulator.indexOf(currentValue) === -1) {
        accumulator.push(currentValue);
    }
    return accumulator;
}, []);

console.log(newArray);

Output:

[ 'Software Engineering', 'Programming', 'Math' ]

Solution 4: Use the map and filter method

You can also use map and filter methods together to remove duplicates from an array.

For example:


let grades = [81 ,85 ,90 ,85 ,81 ,90 ,95];
let map = new Map();
grades.map((i) => { 
    if(!map.has(i)) map.set(i,true);
});
let newArray = [...map.keys()];
console.log(newArray);

Output:

[ 81, 85, 90, 95 ]

Solution 5: forEach loop and a Set

Aside from the four (4) solutions we mentioned above, you can also use a forEach loop and a Set object to keep track of the unique values in the original array.

For example:


let grades = [90 ,87 ,95 ,87 ,90 ,90 ,95];

let set = new Set();
grades.forEach((i) => { ✅
    set.add(i);
});
let newArray = [...set];

console.log(newArray);

Output:

[ 90, 87, 95 ]

What is the most efficient way to remove duplicates from an array in JavaScript?

One of the most efficient ways to remove duplicates from an array in JavaScript is to use the Set object.

A Set is a collection of unique values, so any duplicates are automatically removed when you create a new Set from an array.

For example:

let samplearray = [10, 20, 30, 30, 40, 40, 50];
let newArray = [...new Set(samplearray)]; ✅
console.log(newArray);

As what you have noticed in our given example, we create a new Set object from the original array and then use the spread operator … to convert the Set back into an array.

This method is both easy to use and efficient for removing duplicates from an array in JavaScript.

Here’s the output:

[ 10, 20, 30, 40, 50 ]

Conclusion

In conclusion, this article discusses the several ways to remove duplicates from an array in JavaScript.

And some of the common solutions include using the Set object and the spread operator, the filter method, the reduce method, the map and filter method, and the forEach loop and a Set.

Each of these methods has its own advantages and can be used depending on the specific needs of your code.

One of the most efficient ways to remove duplicates from an array in JavaScript is to use the Set object.

We hope this article has provided you with enough information to help you understand on how to remove duplicates from array JavaScript

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. What is Arrays in JavaScript. Read the ‘What is Arrays in JavaScript?’ section for the details and code.
  2. Different Ways on How to Remove Duplicates from Array in JavaScript. Read the ‘Different Ways on How to Remove Duplicates from Array in JavaScript?’ section for the details and code.
  3. What is the most efficient way to remove duplicates from an array in JavaScript. Read the ‘What is the most efficient way to remove duplicates from an array in JavaScript?’ section for the details and code.
  4. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment