Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To remove duplicates from array in typescript, use the new Set() method with it will create new array without dublicate elements.

The Set object lets you store unique values of any type, whether primitive values or object references.

To use new set in array you have to follow below syntax.

[...new Set(arr)]

Today, I’m going to show you How do I remove duplicates from array in typescript, as above mentioned, I’m going to use the above-mentioned new Set() method to get only unique elements of array.

Let’s start today’s tutorial how do you remove duplicates from array in typescript?

Typescript remove duplicates from array example

Here, we will create an array with some data, and then we will use the new Set() method to remove duplicates elements and store in a different variable.

let arr1: Array<string> = ["Infinitbility", "aguidehub", "sortoutcode", "sortoutcode", "Infinitbility"];
let arr2: Array<string> = [...new Set(arr1)];
console.log(arr2);
// output: [ 'Infinitbility', 'aguidehub', 'sortoutcode' ]

I hope it helps you, All the best đź‘Ť.