Advertisement
deni-parvanov

Untitled

Feb 24th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function deviceNameSolution(devicenames) {
  2.  
  3.     let uniqueNames = new Set(devicenames);
  4.  
  5.     const nameOccurences = new Map(Array.from(uniqueNames).map(n => [n, 0]));
  6.  
  7.     let devices = [];
  8.     for (let deviceName of devicenames) {
  9.         let deviceOccurrenceCount = nameOccurences.get(deviceName);
  10.         if (deviceOccurrenceCount === 0) {
  11.             devices.push(deviceName);
  12.         } else {
  13.             devices.push(deviceName + deviceOccurrenceCount);
  14.         }
  15.  
  16.         nameOccurences.set(deviceName, deviceOccurrenceCount + 1);
  17.     }
  18.  
  19.     return devices;
  20. }
  21.  
  22. deviceNameSolution(['switch', 'tv', 'switch', 'tv', 'tel', 'switch', 'tv']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement