How to find duplicates from list of array in angular 6 using some?

10,237

Solution 1

Maintain counter while checking equality, as every object will be equal with same object, hence check if counter is greater than 1 for getting status.

const status = users.some(user => {
  let counter  = 0;
  for (const iterator of users) {
    if (iterator.name === user.name && iterator.email === user.email && iterator.age === user.age) {
      counter += 1;
    }
  }
  return counter > 1;
});

Solution 2

var users = [
  {
    name: 'John',
    email: '[email protected]',
    age: 25,
  },
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
  },
  {
    name: 'John',
    email: '[email protected]',
    age: 25,
  },
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
  },,
  {
    name: 'Tom',
    email: '[email protected]',
    age: 35,
  },
  {
    name: 'Harry',
    email: '[email protected]',
    age: 23,
  },
  {
    name: 'Kane',
    email: '[email protected]',
    age: 65,
  },
  {
    name: 'Ron',
    email: '[email protected]',
    age: 65,
  },
  {
    name: 'Ron',
    email: '[email protected]',
    age: 65,
  }
];

// complexity of this function is n where n is the no of users
var data = uniqueData(users, 'email');
console.log(data)

function uniqueData(array, key) {
  // create new objects for use
  var uniqueArray = [];
  var map = new Map();

  // loop throught array
  array.forEach((user,index) => {
    // first item is always unique add to unique whithout check
    if(index == 0) {
      // using map set first item in map key and value is dynamic which we can set
      map.set(array[index].email, array[index].email);
      uniqueArray.push(array[index]);
    }

    //check if the key already exists if exists do not push else push
    if (!map.get(user[key])) {
      map.set(user[key], user[key]);
      uniqueArray.push(user);
    }
  });
  return uniqueArray;
}

Share:
10,237
Nishanth
Author by

Nishanth

Updated on June 04, 2022

Comments

  • Nishanth
    Nishanth almost 2 years

    Hi I have list of values in array as bellow

        var users = [{
            name: 'John',
            email: '[email protected]',
            age: 25,
        },
        {
            name: 'Tom',
            email: '[email protected]',
            age: 35,
        },
        {
            name: 'John',
            email: '[email protected]',
            age: 25,
       }];
    

    I should find duplicates row from the above array (need to compare all the fields name, email, and age)

    I have used some function to find a duplicate value as below but need to pass multiple conditions in it. How to do that

     const unique    = new Set();
    
     const showError = this.users.some(element => unique.size === unique.add(element.name).size);
    

    As I have passed the name I need to verify email and age. How to do that

    • Plochie
      Plochie over 4 years
      So u want to just check if array contains duplicate or do u want to get duplicate object as well?
    • Nishanth
      Nishanth over 4 years
      @Plochie I want to check whether the array contains duplicate values
    • Abhishek
      Abhishek over 4 years
      @Nishanth Please check the provide link gist.github.com/telekosmos/3b62a31a5c43f40849bb
  • Nishanth
    Nishanth over 4 years
    I don't want to remove duplicates instead need to return true or false if it contains duplicate row. as well as i will not pass any parameter to the array for finding duplicates