Remove Duplicate Elements from TypeScript Array
Last Updated :
06 Feb, 2024
TypeScript array is a single variable that is used to store the elements or a group of values of the same type. An array can contain multiple occurrences or duplicates of an element that can be removed using the below-discussed methods.
Below are the approaches used to remove Duplicate Elements from the TypeScript Array
The filter() method creates a new array of elements that matches the passed condition through the callback function. It will include only those elements for which true is returned.
Example: The below code uses the filter() method to remove duplicate of an element in TypeScript array.
Javascript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = (arr: number[]): number[] => {
return arr.filter((item,
index) => arr.indexOf(item) === index);
}
console.log(removeDups(arr));
|
Output:
[ 1, 2, 3, 4, 5, 6]
The reduce() method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass.
Example: The reduce() method is used to remove duplicates from an array in TypeScript.
Javascript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = ( arr: number[]): number[] => {
let unique: number[] =
arr.reduce( function (acc: number[], curr: number) {
if (!acc.includes(curr))
acc.push(curr);
return acc;
}, []);
return unique;
}
console.log(removeDups(arr));
|
Output:
[ 1, 2, 3, 4, 5, 6]
Approach 3: Using TypeScript set() Method
This method sets a new object type that allows you to create collections of unique values.
Example: This example uses the TypeScript set() method to remove duplicates from an array.
Javascript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = (arr: number[]): number[] => {
return [... new Set(arr)];
}
console.log(removeDups(arr));
|
Output:
[ 1, 2, 3, 4, 5, 6]
Approach 4: Using TypeScript indexOf() Method
The indexOf() method is used to find the first index of occurrence of an array element. we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the resultant array.
Example: The below code example uses the indexOf() method method to remove duplicates from an array in TypeScript.
Javascript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = (arr: number[]) : number[] => {
let unique: number[] = [];
for (let i = 0; i < arr.length; i++) {
if (unique.indexOf(arr[i]) === -1) {
unique.push(arr[i]);
}
}
return unique;
}
console.log(removeDups(arr));
|
Output:
[ 1, 2, 3, 4, 5, 6]
Approach 5: Using TypeScript forEach() Method
By using the forEach() method, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array.
Example: The forEach() method is used to remove the elements from the TypeScript array in the below code.
Javascript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = (arr: number[]) : number[] => {
let unique: number[] = [];
arr.forEach(element => {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique;
}
console.log(removeDups(arr));
|
Output:
[ 1, 2, 3, 4, 5, 6]
Share your thoughts in the comments
Please Login to comment...