3

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

3
  • So u want to just check if array contains duplicate or do u want to get duplicate object as well? Commented Jan 2, 2020 at 11:42
  • @Plochie I want to check whether the array contains duplicate values Commented Jan 2, 2020 at 11:51
  • @Nishanth Please check the provide link gist.github.com/telekosmos/3b62a31a5c43f40849bb Commented Jan 2, 2020 at 12:03

5 Answers 5

4

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;
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

Comments

0

Use below code for remove duplicates

function removeDuplicates(array, key) { let lookup = {}; return array.filter(obj => !lookup[obj[key]] && lookup[obj[key]] = true); }

1 Comment

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
0

Try this:

const showError: boolean = Array.from(new Set(users.map(user => JSON.stringify(user)))).length != users.length;

Comments

0
var frequency = users.reduce(function(seen, currentItem) {
    if (currentItem in seen) {
        seen[currentItem] = seen[currentItem] + 1;
    } else {
        seen[currentItem] = 1;
    }
    return seen;
}, {});

for (var key in frequency) {
    if (frequency[key] > 1) {
        result.push(key.split(",").map(function(currentItem) {
            return parseInt(currentItem);
        }));
    }
}

console.log(result);

hope this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.