To remove duplicates from an array in TypeScript, you can use similar approaches as in JavaScript due to TypeScript's compatibility with JavaScript.
In this example,
let numbers: number[] = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers: number[] = [...new Set(numbers)];
console.log('Array without duplicates:', uniqueNumbers);
Array without duplicates: {"0":1,"1":2,"3":3,"4":4,"6":5}
In this example,
let colors: string[] = ['red', 'green', 'blue', 'green', 'yellow', 'blue'];
let uniqueColors: string[] = colors.filter((value, index, self) => self.indexOf(value) === index);
console.log('Array without duplicates:', uniqueColors);
Array without duplicates: {"0":"red","1":"green","2":"blue","4":"yellow"}
In this tutorial, we learned How to Remove Duplicates in an Array in TypeScript language with well detailed examples.