svephoto

Serialize String [JavaScript]

Nov 10th, 2021 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function serializeString(inputArray) {
  2.     let string = inputArray[0];
  3.     let letters = [];
  4.  
  5.     for (let i = 0; i < string.length; i++) {
  6.         if (!letters.includes(string[i])) {
  7.             letters.push(string[i]);
  8.         }
  9.     }
  10.  
  11.     while (letters.length > 0) {
  12.         let char = letters.shift();
  13.         let indices = [];
  14.  
  15.         for (let i = 0; i < string.length; i++) {
  16.             if (char === string[i]) {
  17.                 indices.push(i);
  18.             }
  19.         }
  20.  
  21.         console.log(char + ':' + indices.join('/'));
  22.     }
  23. }
  24.  
Add Comment
Please, Sign In to add comment