0

What is the best way to return information about duplicates in typescript from a large array of objects (I want it to return true or false).

let testArray: { id: number, name: string }[] = [
    { "id": 0, "name": "name1" },
    { "id": 1, "name": "name2" },
    { "id": 2, "name": "name3" },
    { "id": 3, "name": "name4" },
    { "id": 2, "name": "name3" },
    { "id": 2, "name": "name3" }
];
2
  • true or false, if any duplicate exists? or... would it be better to just dedupe the array in the first place.. The latter most certainly has been asked and answered here before Commented Feb 22, 2024 at 18:54
  • 1
    Is this as complicated as your data gets? Are the keys always in the same order? You could potentially stringify each array element and throw it into a set and compare the size of the set vs the original array. Commented Feb 22, 2024 at 18:54

3 Answers 3

1

You can go filter your array and create your ruleset how to identify duplicates and then handle in the way you want.

let testArray: { id: number; name: string }[] = [
      { id: 0, name: 'name1' },
      { id: 1, name: 'name2' },
      { id: 2, name: 'name3' },
      { id: 3, name: 'name4' },
      { id: 2, name: 'name3' },
      { id: 2, name: 'name3' },
    ];

this.totalDuplicates = testArray.filter((item, index) => 
    testArray.some((elem, idx) => (elem.id === item.id && elem.name === item.name)&& idx !== index)).length;

Example on stackbiz: https://stackblitz.com/edit/stackblitz-starters-8fiqaa?file=src%2Fmain.ts

Good way is when you can implement isEqual() function for your data model, then utilize it for controllable comparison. And it will be located in proper spot together with you data model class.

Sign up to request clarification or add additional context in comments.

Comments

1
let testArray: { id: number, name: string }[] = [
    { "id": 0, "name": "name1" },
    { "id": 1, "name": "name2" },
    { "id": 2, "name": "name3" },
    { "id": 3, "name": "name4" },
    { "id": 2, "name": "name3" },
    { "id": 2, "name": "name3" }
];

function hasDupes(array: { id: number, name: string }[]): boolean {
    const filteredArray = array.filter((item, index, self) =>
        index !== self.findIndex(t => (
            t.id === item.id && t.name === item.name
        ))
    );
    return filteredArray.length !== array.length;
}

console.log(hasDupes(testArray)); // Output: true

So, filter() is to create an array that only has the duplicates.

findIndex() is to find the index of the first occurrence of each object in the array. If index of the current object is not equal to the first occurrence index, then it is a duplicated.

If the lengths of the original array and the filtered array are different, it indicates that duplicates were found, and the function returns true. Otherwise, it returns false.

Comments

1

My approach would be using a Set, which would guarantee that no item is a duplicate. Then you could compare the size of your array and your set:

let testArray: { id: number, name: string }[] = [
    { "id": 0, "name": "name1" },
    { "id": 1, "name": "name2" },
    { "id": 2, "name": "name3" },
    { "id": 3, "name": "name4" },
    { "id": 2, "name": "name3" },
    { "id": 2, "name": "name3" }
];

const testSet = new Set(...testArray)
const hasDuplicates = testArray.length > testSet.size

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.