Remove elements from an array. Logic of an array - present and constant time.

Sort by number of letters. Check words starting with the same letter. Verify that the word is fully present in another word. When both begin with the same letter. If it is fully present. Show only the first word and ignore the second word

var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying   ", "eating", "smiling","run", "walk", "push", "cook", "cry", "eat"];

Waiting for output using javascript / jquery:

I need to remove these words: run, walk, push, cook, cry, eat.

var arrayList = ["run", "walk", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "push", "drinking", "dancing", "cook", "cry", "eat", "smiling","run", "walk", "push", "cook", "cry", "eat"];
+4
source share
2 answers

for the case mentioned by @Grimbode From OP

Verify that the word is fully present in another word.

So a smile is not completely present in a smile

,

function filter(arr) {
  arr.sort(function(a, b) {
    return a.length - b.length;
  })
  return arr.reduce(function(acc, el) {
    if (!acc.some(function(e) {
      return el.startsWith(e);
    })) {
      acc.push(el);
    }
    return acc;
  }, []);
}

: Polyfill mdn

if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(searchString, position) {
        position = position || 0;
        return this.lastIndexOf(searchString, position) === position;
    };
}

if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(searchString, position) {
        position = position || 0;
        return this.lastIndexOf(searchString, position) === position;
    };
}

var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying   ", "eating", "smiling", "run", "walk", "push", "cook", "cry", "eat"];

function filter(arr) {
  arr.sort(function(a, b) {
    return a.length - b.length;
  })
  return arr.reduce(function(acc, el) {
    if (!acc.some(function(e) {
      return el.startsWith(e);
    })) {
      acc.push(el);
    }
    return acc;
  }, []);
}
document.getElementById('before').innerHTML = JSON.stringify(arraylist);

document.getElementById('result').innerHTML = JSON.stringify(filter(arraylist));
<div><span>initial array:</span><span id="before"></span>
  <div>
    <div><span>result array:</span><span id="result"></span>
      <div>
Hide result
+1

. , @Grimbode

var sentence = "Did Marying go to the store today?";
var arraylist = ["running", "walking", "standing", "writing", "waiting", "sleeping", "reading", "washing", "sitting", "riding", "shopping", "singing", "painting", "watching", "swimming", "pulling", "smoking", "pushing", "drinking", "dancing", "cooking", "crying   ", "eating", "smiling","run", "walk", "push", "cook", "cry", "eat"];


function myFunction() {
    $.each(arraylist, function(index,item){
       $.each(arraylist,function(indexInner, item1){
           if( index != indexInner && item1 && item && item1.substring(0,(item.length)) == item){  
             arraylist[indexInner] = arraylist[index];
             arraylist[index] = false;
           }
       });
    });
    arraylist = cleanArray(arraylist);
    document.getElementById("demo").innerHTML = arraylist;
}


function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Click the button to add elements to the array.</p>

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
Hide result
0

Source: https://habr.com/ru/post/1583949/


All Articles