Open In App

Checking for Duplicate Strings in JavaScript Array

Checking for duplicate strings in a JavaScript array involves identifying if there are any repeated string values within the array. Given an array of strings a with size N, find all strings that occur more than once. If none is found, return [-1].

Example:

Input: 
N = 4
arr = ['apple', 'banana', 'orange', 'apple']

Output :
[ 'apple' ]

Explanation :
There is only 'apple' is repeating 2 times therefore output is 'apple' in given array.

Using Iteration

In this approach, iterate through the array and compare each element with every other element to detect duplicates. This method has a time complexity of O(n^2) as it involves nested iteration over the array, making it less efficient for large datasets.

Example: Implementation to check for duplicate strings in JavaScript array using simple iteration.

function duplicateStr(arr) {
    let res = [];
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i] === arr[j]) {
                res.push(arr[i]);
            }
        }
    }
    if (res.length == 0) res.push(-1);
    return res;
}

const array = ['apple', 'banana', 'orange', 'apple'];
console.log(duplicateStr(array));

Output
[ 'apple' ]

Time complexity: 0( n2 )

Space complexity: 0( 1 )

Using Set

Utilize a Set data structure to store unique elements. While iterating through the array, check if the element already exists in the Set.

Example: Implementation to check for duplicate strings in JavaScript array using set.

function duplicatesStr(arr) {
    const set = new Set();
    const res = [];
    for (let item of arr) {
        if (set.has(item)) {
            res.push(item);
        }
        else set.add(item);
    }
    res.length == -1 && res.push(-1);
    return res;
}

const array = ["apple", "orange", "apple", "mango", "banana", "kiwi", "banana"];
console.log(duplicatesStr(array));  

Output
[ 'apple', 'banana' ]

Time complexity: 0( n )

Space complexity: 0 ( n )

Using Sorting

In the sorting approach, begin by sorting the array. Then, iterate through it and check if adjacent elements are identical. This method offers a time complexity of O(n log n) due to the sorting operation, followed by a linear iteration to identify duplicates, providing an efficient means for duplicate detection in arrays.

Example: Implementation to check for duplicate strings in JavaScript array using sorting method.

function duplicatesStr(arr) {
    arr.sort();
    const res = [];
    for (let i = 0; i < arr.length - 1; i++) {
        let flage = false;
        while (i < arr.length && arr[i] === arr[i + 1]) {
            flage = true;
            i++;
        }
        if (flage) res.push(arr[i]);
    }
    res.length == 0 && res.push(-1);
    return res;
}

const array = ["apple", "banana", "orange", 
    "apple", "mango", "banana", "kiwi", "banana"];
console.log(duplicatesStr(array));

Output
[ 'apple', 'banana' ]

Time complexity: 0(n log n)

Space complexity: 0( 1 )

Frequency counting

In the frequency counting approach, utilize an object to store the frequency of each string in the array. Then, return an array of strings that have a frequency greater than one. This method offers a time complexity of O(n) as it involves a single iteration over the array to count frequencies, providing an efficient solution for identifying duplicate strings.

Example: Implementation to check for duplicate strings in JavaScript array using frequency counting method.

function duplicatesStr(arr) {
    const freqMap = {};
    for (let item of arr) {
        if (item in freqMap) freqMap[item]++;
        else freqMap[item] = 1;
    }

    const res = [];
    for (const key in freqMap) {
        if (freqMap[key] > 1) res.push(key);
    }
    if (res.length == 0) res.push(-1);
    return res;
}

const array = ["apple", "banana", "orange", "apple",
    "mango", "banana", "apple", "banana", "orange",
    "apple", "mango", "banana"];
console.log(duplicatesStr(array)); 

Output
[ 'apple', 'banana', 'orange', 'mango' ]

Time complexity: 0( n )

Space complexity: 0 ( n )

Article Tags :