Open In App

How to Create a TypeScript Function to Check for Duplicates in an Array ?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given a TypeScript array and we have to create a TypeScript function that checks whether the given array contains duplicates or not.

Example:

Input: array1 = [1, 2, 3, 4, 5]
Output: False
Explantion: No duplicates in the array
Input: array1 = [1, 2, 3, 4, 1]
Output: True
Explantion: 1 repeats two time in the array

Using a Set to Create the Function

In this method, we create a Set from the array, which automatically removes duplicate values. After which we compares the size of the Set to the length of the original array. If they are different, then the array contains duplicates.

Syntax:

new Set(array_name).size !== array_name.length;

Example: The below code explains the use of the Set to create a function that checks for duplicates in an array.

Javascript




function hasDuplicatesUsingSet(array: any[]):
    void {
    const mySet = new Set(array);
    if (mySet.size !== array.length) {
        console.log("The Array: [" + array +
            "] contains duplicate elements.");
    }
    else {
        console.log("The Array: [" + array +
            "] does not contain duplicate elements.");
    }
}
hasDuplicatesUsingSet([1, 2, 3, 4, 5]);
hasDuplicatesUsingSet([1, 2, 3, 4, 1]);
hasDuplicatesUsingSet(["GFG", "JavaScript", "GFG"]);
hasDuplicatesUsingSet(["GFG", "JavaScript"]);


Output:

The Array: [1,2,3,4,5] does not contain duplicate elements.
The Array: [1,2,3,4,1] contains duplicate elements.
The Array: [GFG,JavaScript,GFG] contains duplicate elements.
The Array: [GFG,JavaScript] does not contain duplicate elements.

Using Array.prototype.includes()

In this method, we will iterate over each element of the array and checks if the array includes the same element later in the array (starting from the next index). If such an element is found, it returns true, indicating duplicates.

Syntax:

array.some((item, index) => array.includes(item, index + 1));

Example: The below code explains the use of Array.prototype.includes() to create a function that checks for duplicates in an array.

Javascript




function hasDuplicatesUsingIncludes(array: any[]): void {
    const isDuplicate = array.
        some((item, index) => array.
            includes(item, index + 1));
    if (isDuplicate) {
        console.log("The Array: [" + array +
            "] contains duplicate elements.");
    }
    else {
        console.log("The Array: [" + array +
            "] does not contain duplicate elements.");
    }
}
hasDuplicatesUsingIncludes([1, 2, 3, 4, 5]);
hasDuplicatesUsingIncludes([1, 2, 3, 4, 1]);
hasDuplicatesUsingIncludes(["GFG", "JavaScript", "GFG"]);
hasDuplicatesUsingIncludes(["GFG", "JavaScript"]);


Output:

The Array: [1,2,3,4,5] does not contain duplicate elements.
The Array: [1,2,3,4,1] contains duplicate elements.
The Array: [GFG,JavaScript,GFG] contains duplicate elements.
The Array: [GFG,JavaScript] does not contain duplicate elements.

Using Array.prototype.reduce()

In this method, we initializes an empty object (counter) to keep track of the count of each element and than we iterates over each element of the array and increment its count in the counter object. If the count becomes greater than 1 for any element, it indicates duplicates, and the function returns true.

Example: The below code explains the use Array.prototype.reduce() with an object as a counter to check for duplicates in an array.

Javascript




function hasDuplicatesUsingReduce
    (array: any[]): boolean {
    const counter:
        { [key: string]: number } = {};
    for (const item of array) {
        counter[item] =
            (counter[item] || 0) + 1;
        if (counter[item] > 1) {
            return true;
        }
    }
    return false;
}
console.log
    (hasDuplicatesUsingReduce([1, 2, 3, 4, 5]));
console.log
    (hasDuplicatesUsingReduce([1, 2, 3, 4, 1]));
console.log
    (hasDuplicatesUsingReduce(["GFG", "JavaScript", "GFG"]));
console.log
    (hasDuplicatesUsingReduce(["GFG", "JavaScript"]));


Output:

false
true
true
false


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads