How to get the difference of two string arrays?

前端 未结 3 1074
猫巷女王i
猫巷女王i 2021-01-15 05:51

I want to get the exact difference between two string arrays.

const array1 = [\'T\',\'E\',\'A\',\'P\',\'A\',\'P\',\'E\',\'R\'];
const array2 = [\'T\',\'A\',\         


        
3条回答
  •  一生所求
    2021-01-15 06:27

    You could remove elements from allowed based on the input array:

     const allowed = ['T','E','A','P','A','P','E','R'];
     const input = ['T','A','P'];
    
     for(const char of input) {
       const pos = allowed.indexOf(char);
       if(pos === -1) {
         // char doesnt exist?
       } else {
         allowed.splice(pos, 1);
       }
     }
    

    Then allowed will be your expected result at the end.

提交回复
热议问题