Vassil_Iliev

first time

Apr 16th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const checkIfMirror = string => {
  2.     const arr = string.split('').sort();
  3.     let removed = []
  4.     let index = 1;
  5.  
  6.     console.log(`${arr} <=========== sorted`)
  7.  
  8.     while (index <= arr.length) {
  9.  
  10.         if (arr[index] !== arr[index - 1] && arr[index] !== arr[index + 1]) {
  11.             removed.push(arr.splice(arr.indexOf(arr[index]), 1));
  12.  
  13.         } else if (arr[arr.length - 1] !== arr[arr.length - 2]) {
  14.             removed.push(arr.splice(arr.indexOf(arr[arr.length - 1])))
  15.  
  16.         } else if (arr[0] !== arr[1]) {
  17.             removed.push(arr.splice(0, 1))
  18.         }
  19.         index++
  20.     }
  21.  
  22.     index = 0;
  23.  
  24.     while (index <= arr.length) {
  25.  
  26.         if (arr[index] !== arr[index - 1] && arr[index] !== arr[index + 1]) {
  27.             removed.push(arr.splice(arr.indexOf(arr[index]), 1));
  28.  
  29.         } else if (arr[arr.length - 1] !== arr[arr.length - 2]) {
  30.             removed.push(arr.splice(arr.indexOf(arr[arr.length - 1])))
  31.  
  32.         } else if (arr[0] !== arr[1]) {
  33.             removed.push(arr.splice(0, 1))
  34.         }
  35.         index++
  36.     }
  37.  
  38.     console.log(`${arr} <=========== cleaned`)
  39.  
  40.     console.log(`${removed.join()}<========= removed`)
  41.  
  42.     let curSequence = 0;
  43.     let longestSeq = 0;
  44.  
  45.     for (let i = 0; i < arr.length; i++) {
  46.  
  47.         if (arr[i] === arr[i + 1]) {
  48.             curSequence++
  49.         } else {
  50.  
  51.             if (curSequence > longestSeq) {
  52.                 longestSeq = curSequence;
  53.                 curSequence = 0;
  54.             } else {
  55.                 curSequence = 0;
  56.             }
  57.  
  58.         }
  59.     }
  60.  
  61.     console.log(`${longestSeq} <========== longest seq`)
  62.  
  63.     if (arr.length % 2 === 0 && removed.length <= 1) {
  64.         return true;
  65.     } else if (arr.length % 2 !== 0 && removed.length === 0) {
  66.         return true;
  67.     } else {
  68.         return false;
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment