0

I have an array of objects where properties may share the same values:

const arr = [
  {name: 'Stephan', phone: 123},
  {name: 'Stephan', phone: 432},
  {name: 'Begal', phone: 432},
  {name: 'Lucy', phone: 777},
  {name: 'Angel', phone: 432},
  {name: 'Lucy', phone: 555},
  {name: 'Mike', phone: 999},
];

(You can observe that either the name or the phone can be repeated more than once).

I need to extract the objects that have duplicated phone/name in a separate array/object for further manipulation.

Ideally, the array of duplicates generated from the example above could look like this:

const dups = [
  [
    {name: 'Stephan', phone: 123},
    {name: 'Stephan', phone: 432},
    {name: 'Begal', phone: 432},
    {name: 'Angel', phone: 432},
  ],
  [
    {name: 'Lucy', phone: 777},
    {name: 'Lucy', phone: 555},
  ]
];

How can achieve that?

My algorithm knowledge is not strong, but I imagine that sorting the original array by both name and phone would be the first step?

1 Answer 1

1

No need to sort. Iterate the array and maintain two helper indexes that say "name/phone X is in the group Y":

const arr = [
    {name: 'Stephan', phone: 123},
    {name: 'Stephan', phone: 432},
    {name: 'Begal', phone: 432},
    {name: 'Lucy', phone: 777},
    {name: 'Angel', phone: 432},
    {name: 'Lucy', phone: 555},
    {name: 'Mike', phone: 999},
];


var groups = [[]],
    names = {},
    phones = {};

for (var obj of arr) {
    var g = names[obj.name] || phones[obj.phone];
    if (!g)
        groups[g = groups.length] = [];
    groups[g].push(obj);
    names[obj.name] = phones[obj.phone] = g;
}

console.log(groups.slice(1))

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

3 Comments

Why did you initially add an empty array here var groups = [[]]?
@marzelin: just to make the !g check simpler (otherwise I'd have to check for undefineds).
Yeah, 0 is a falsy value. Clever.

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.