how to remove duplicates in a string array in typescript in javascript

index.ts
const removeDuplicates = (arr: string[]): string[] => {
    return arr.filter((item, index) => arr.indexOf(item) === index);
};

// Example
const arr = ['apple', 'banana', 'orange', 'apple', 'banana'];
const uniqueArr = removeDuplicates(arr);
console.log(uniqueArr); // Output: ['apple', 'banana', 'orange']
308 chars
9 lines

related categories

gistlibby LogSnag