by Sai gowtham

How to remove duplicate elements from array JavaScript

In this tutorial, we will learn about how to remove duplicate elements from an array.

First way

We are using JavaScript new Set Object to store the unique elements from the array.

const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32];

console.log([...new Set(numbers)]);

// [2, 3, 4, 5, 6, 7, 32]

Second way

Array.map() method.

const jobs = ['Web Developer','Marketing Manager','Project   Manager','Web Developer'];

function getUnique(arr){

  const final = [];

  arr.map((e,i)=> !final.includes(e) && final.push(e) )

  return final
}

console.log(getUnique(jobs))

//["Web Developer", "Marketing Manager", "Project Manager"]

In the above code, on each iteration, we check if an element is not present in the final array then we are pushing that element to the final array.

Third way

const jobs = ['Web Developer','Marketing Manager','Project Manager','Web Developer']

function getUnique(arr){

 return arr.filter((e,i)=> arr.indexOf(e) >= i)
}

console.log(getUnique(jobs))

//["Web Developer", "Marketing Manager", "Project Manager"]

On each iteration, we are using the Array.indexOf method to get the index of that element. if the returned index is greater than the current index then we will add that element to a new array and return it.

Happy coding…

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2022 (Beginner + Advanced)
JavaScript - The Complete Guide 2022 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY