DimiPetrov

Neighborhoods - Associative Arrays

Dec 7th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. function solve(input) {
  2. let map = new Map();
  3. let neighborhoods = input.shift().split(', ');
  4.  
  5. for(let neighborhood of neighborhoods) {
  6. map.set(neighborhood, []);
  7. }
  8.  
  9. for(let i = 0; i < input.length; i++) {
  10. let current = input.shift().split(' - ');
  11. let neighborhood = current[0];
  12. let person = current[1];
  13. if(neighborhoods.includes(neighborhood)) {
  14. map.get(neighborhood).push(person);
  15. }
  16. }
  17.  
  18. let sorted = Array.from(map).sort((a, b) => {b[0] - a[0]});
  19. let neighborhoodName = sorted[0];
  20. let persons = sorted[1];
  21. for(let neighborhoodName of sorted) {
  22. console.log(`${neighborhoodName}: ${persons.length}`);
  23. for(let j = 0; j < persons.length; j++) {
  24. console.log(`--${persons[j]}`);
  25. }
  26. }
  27. }
  28.  
  29. solve(['Abbey Street, Herald Street, Bright Mews',
  30. 'Bright Mews - Garry',
  31. 'Bright Mews - Andrea',
  32. 'Invalid Street - Tommy',
  33. 'Abbey Street - Billy'])
Advertisement
Add Comment
Please, Sign In to add comment