maymunskoa

07. Company Users (Assoc Arrays)

Mar 7th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let company = new Map();
  4.  
  5. for (const line of input) {
  6.     let [companyName, person] = line.split(' -> ');
  7.     let namesList = [];
  8.     if(!company.has(companyName)){
  9.         namesList.push(person);
  10.         company.set(companyName, namesList);
  11.     }else{
  12.         let currentList = company.get(companyName);
  13.         currentList.push(person);
  14.         company.set(companyName, Array.from(new Set(currentList)));
  15.     }
  16. }
  17.  
  18.     Array.from(company).sort((a,b)=>{
  19.         return a[0].localeCompare(b[0]);
  20.     }).forEach(pair=>{
  21.         console.log(`${pair[0]}`);
  22.         pair[1].forEach(user=>{
  23.             console.log(`-- ${user}`);
  24.         })      
  25.        
  26.     })
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment