Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the array index of duplicates

In a JavaScript array how can I get the index of duplicate strings?

Example:

MyArray = ["abc","def","abc"]; //----> return 0,2("abc");

Another example:

My Array = ["abc","def","abc","xyz","def","abc"] 
//----> return 0,2,5("abc") and 1,4("def");

I have no idea how to do this. Thanks in advance for your help!

like image 499
qwertyuiop Avatar asked Aug 24 '13 10:08

qwertyuiop


People also ask

How do you find index duplicates in array?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.

How do I check if an array has duplicates?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do you find duplicates in a 2d array?

Here's a brute-force straight forward way of counting duplicates. Turn the 2d array into a 1d array ( List<Integer> ), then loop through the 1d array counting the duplicates as you find them and removing them so you don't count them more than once.

Can index have duplicates?

Although indexes may have duplicate keys, there are important things to be aware about. Are the duplicates both nonclustered? You may have a case where one of your duplicates is the clustered index— and that means that index is the data in the table itself.


1 Answers

Update 01/2022: It's not 2013 anymore, and many things have changed. I neither recommend modifying the prototype, nor is the approach in this answer the "best" as it requires several iterations over the array.

Here's an updated version of the original answer, retaining its spirit, as well as the original answer below.

function getDuplicates<T>(input: T[]): Map<T, number[]> {
    return input.reduce((output, element, idx) => {
        const recordedDuplicates = output.get(element);
        if (recordedDuplicates) {
            output.set(element, [...recordedDuplicates, idx]);
        } else if (input.lastIndexOf(element) !== idx) {
            output.set(element, [idx]);
        }

        return output;
    }, new Map<T, number[]>());
}

Yet another approach:

Array.prototype.getDuplicates = function () {
    var duplicates = {};
    for (var i = 0; i < this.length; i++) {
        if(duplicates.hasOwnProperty(this[i])) {
            duplicates[this[i]].push(i);
        } else if (this.lastIndexOf(this[i]) !== i) {
            duplicates[this[i]] = [i];
        }
    }

    return duplicates;
};

It returns an object where the keys are the duplicate entries and the values are an array with their indices, i.e.

["abc","def","abc"].getDuplicates() -> { "abc": [0, 2] }
like image 108
Ingo Bürk Avatar answered Oct 09 '22 21:10

Ingo Bürk