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?