SHARE
TWEET

Followers

a guest Oct 11th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function followers(input) {
  2.  
  3.     let users = new Map();
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.         let data = input[i];
  7.  
  8.         if (data.startsWith("Welcome, ")) {
  9.             data = data.split("Welcome, ")[1];
  10.  
  11.             if (!users.has(data)) {
  12.                 users.set(data, [[], []]); //[following list,followers list]
  13.             }
  14.         } else {
  15.             data = data.split(" followed ");
  16.  
  17.             let firstUser = data[0];
  18.             let secondUser = data[1];
  19.  
  20.             if (users.has(firstUser)
  21.                 && users.has(secondUser)
  22.                 && firstUser !== secondUser) {
  23.  
  24.                 let followers = users.get(firstUser)[0];
  25.                 let following = users.get(secondUser)[1];
  26.  
  27.                 if (!following.includes(firstUser)
  28.                     && !followers.includes(secondUser)) {
  29.  
  30.                     users.get(firstUser)[0].push(secondUser);
  31.                     users.get(secondUser)[1].push(firstUser);
  32.                 }
  33.             }
  34.         }
  35.     }
  36.  
  37.     console.log(`Total users registered: ${users.size}`);
  38.  
  39.     let orderedUsers = Array.from(users)
  40.         .sort((a, b) => b[1][1].length - a[1][1].length
  41.             || b[0].localeCompare(a[0]));
  42.  
  43.     for (let i = 1; i <= orderedUsers.length; i++) {
  44.  
  45.         let user = orderedUsers[i - 1];
  46.  
  47.         console.log(`${i}. ${user[0]} : ${user[1][0].length} following, ${user[1][1].length} followers`);
  48.  
  49.         if (i === 1) {
  50.             let followers = Array.from(user[1][1]).sort();
  51.  
  52.             for (let follower of followers) {
  53.                 console.log(`*  ${follower}`);
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. followers(["Welcome, EmilConrad",
  60.     "Welcome, VenomTheDoctor",
  61.     "Welcome, Saffrona",
  62.     "Saffrona followed EmilConrad",
  63.     "Saffrona followed VenomTheDoctor",
  64.     "EmilConrad followed VenomTheDoctor",
  65.     "VenomTheDoctor followed VenomTheDoctor",
  66.     "Saffrona followed EmilConrad"]);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top