Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- const log = new Map();
- const subscriptions = new Map();
- let count = 1;
- args.forEach((el) => {
- const currentCommand = el.split('-');
- if (currentCommand.length === 2) {
- if (log.has(currentCommand[0]) && log.has(currentCommand[1])) {
- log.get(currentCommand[1]).add(currentCommand[0]);
- subscriptions.get(currentCommand[0]).add(currentCommand[1]);
- }
- } else if (!log.has(currentCommand[0])) {
- log.set(currentCommand[0], new Set());
- subscriptions.set(currentCommand[0], new Set());
- }
- });
- const sortedLog = new Map([...log.entries()].sort((firstEntry, secondEntry) => {
- const firstEntryName = firstEntry[0];
- const firstEntrySubscribers = firstEntry[1].size;
- const secondEntryName = secondEntry[0];
- const secondEntrySubscribers = secondEntry[1].size;
- let result = secondEntrySubscribers - firstEntrySubscribers;
- if (result === 0) {
- const firstEntrySubscriptions = subscriptions.get(firstEntryName).size;
- const secondEntrySubscriptions = subscriptions.get(secondEntryName).size;
- result = secondEntrySubscriptions - firstEntrySubscriptions;
- }
- return result;
- }));
- const importPerson = [...sortedLog.entries()][0];
- console.log(importPerson[0]);
- importPerson[1].forEach((e) => {
- console.log(`${count}. ${e}`);
- count++;
- });
- }
Add Comment
Please, Sign In to add comment