Remove Duplicate Elements from TypeScript Array
Last Updated :
01 Aug, 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]
Approach 6: Using TypeScript Map
The Map object holds key-value pairs and remembers the original insertion order of the keys. We can leverage this feature to remove duplicates from an array.
Example: In the following example, we utilize a Map to remove duplicate elements from a TypeScript array.
JavaScript
let arr = [1, 2, 2, 3, 4, 3, 5, 4, 6, 5];
const removeDups = (arr: number[]): number[] => {
const map = new Map<number, boolean>();
const unique: number[] = [];
arr.forEach(item => {
if (!map.has(item)) {
map.set(item, true);
unique.push(item);
}
});
return unique;
}
console.log(removeDups(arr));
Output:
[1, 2, 3, 4, 5, 6]
Approach 7: Using TypeScript Sort() and Reduce() Methods Together
Another approach to remove duplicates from an array in TypeScript involves first sorting the array and then using the reduce() method to filter out the duplicates. This method can be efficient, especially for large arrays, as it reduces the complexity by ensuring only adjacent elements need to be compared.
Example: The below code uses the sort() and reduce() methods together to remove duplicates from a TypeScript array.
JavaScript
let array: number[] = [5, 3, 2, 1, 4, 5, 3, 6, 2];
array.sort((a, b) => a - b);
let uniqueArray = array.reduce((acc: number[], currentValue: number) => {
if (acc.length === 0 || acc[acc.length - 1] !== currentValue) {
acc.push(currentValue);
}
return acc;
}, []);
console.log(uniqueArray);
Output:
[ 1, 2, 3, 4, 5, 6 ]
Similar Reads
Remove Duplicate Elements from JavaScript Array
To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
Remove duplicate elements from an array using AngularJS
We have given an array and the task is to remove/delete the duplicates from the array using AngularJS. Approach: The approach is to use the filter() method and inside the method, the elements that don't repeat themselves will be returned and the duplicates will be returned only once.Hence, a unique
2 min read
How to Remove Duplicate Elements from an Array using Lodash ?
Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un
3 min read
How to Remove duplicate elements from array in JavaScript ?
In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
4 min read
Golang Program that Removes Duplicate Elements From the Array
Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequen
4 min read
How to Remove Duplicates from an Array of Objects using TypeScript ?
We are given an array of objects and we have to check if there are duplicate objects present in the array and remove the duplicate occurrences from it. Otherwise, return the array as it is. Examples of Removing Duplicates from an Array of Objects using TypeScript Table of Content Using filter() and
4 min read
How do I Remove an Array Item in TypeScript?
In this article, we will learn about the different ways of removing an item from an array in TypeScript. In TypeScript, an array can be defined using union typing if it contains items of different types. We can use the following methods to remove items from a TypeScript array: Table of Content Using
5 min read
TypeScript Array flat() Method
The Array.prototype.flat() method in TypeScript allows you to create a new array with all sub-array elements concatenated into it, up to a specified depth. This method helps in flattening nested arrays to the desired depth, making it easier to handle deeply nested structures. Syntax:array.flat([dept
3 min read
How to Select a Random Element from Array in TypeScript ?
We are given an array in TypeScript and we have to select a random element every time the code runs. It will automatically select a new random element every time and return it. The below approaches can be used to accomplish this task: Table of Content Using Math.random() functionUsing splice() metho
2 min read
Array filter() Method - TypeScript
The Array.filter() method in TypeScript creates a new array with elements that pass the test provided by a callback function. It does not modify the original array and accepts an optional thisObject for context within the callback function. Syntaxarray.filter(callback[, thisObject])Parameter: This m
3 min read