Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Last active October 14, 2024 11:47
Show Gist options
  • Save nsdevaraj/07c334b94263fd9870cec0ca264f7a49 to your computer and use it in GitHub Desktop.
Save nsdevaraj/07c334b94263fd9870cec0ca264f7a49 to your computer and use it in GitHub Desktop.
input
1 - 2
1 - 3
2 - 3
2 - 4
3 - 1
3 - 2
4 - 1
4 - 2
sort:
1 - 2
1 - 3
2 - 3
2 - 4
1 - 3
2 - 3
1 - 4
2 - 4
prob
1-4
2-4
required
1 - 3
2 - 3
3 - 2
4 - 2
let data = {
links: [{
from: 1,
to: 2,
}, {
from: 1,
to: 3,
}, {
from: 2,
to: 3,
}, {
from: 2,
to: 4,
},
{
from: 3,
to: 1,
}, {
from: 3,
to: 2,
}, {
from: 4,
to: 1,
}, {
from: 4,
to: 2,
}
]
}
var keyVals = [];
var influencers = [];
for (let index = 0; index < data.links.length; index++) {
const element = data.links[index];
var pairs = [];
pairs.push(element.from, element.to);
pairs.sort((a, b) => a - b);
keyVals.push({ id: pairs[0], value: pairs[1] });
}
keyVals.sort((a, b) => a.id - b.id);
console.log(keyVals);
for (let index = 0; index < keyVals.length; index++) {
if (!influencers.find((influencer) => influencer.id === keyVals[index].id)) {
influencers.push({ id: keyVals[index].id, count: 1 });
} else {
influencers.find(
(influencer) => influencer.id === keyVals[index].id
).count += 1;
}
}
console.log(influencers);
Implement a function that returns an object showing how many unique connections each user has.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment