4

Sort by count of letters.Check for words that begin with the same letter.Check if a word is completely present in another word.when both begin with the same letter.If it is completely 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"];

Expecting Output using javascript/jquery:

I need to remove these words: running, walking, pushing, cooking, crying, eating.

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"];
RSKMR
  • 1,702
  • 1
  • 22
  • 54
  • Remove might not be the right term. It seems you just want to "replace" them. – kemicofa ghost Apr 22 '15 at 07:29
  • follow the link. my formating was blown when the page pushed my reply into comment due to shortness http://stackoverflow.com/questions/5767325/remove-specific-element-from-an-array – user3154108 Apr 22 '15 at 07:34
  • 2
    @user3154108 whaaaat? – George Garchagudashvili Apr 22 '15 at 07:34
  • @GeorgeGarchagudashvili It appears to have been automatically converted to a comment from an answer. – Scimonster Apr 22 '15 at 07:36
  • It seems it is not as simple as you think @user3154108. For example what if the word is 'smile' ? smile can not be found within smiling. Levenshtein distance algo will have to be used? – kemicofa ghost Apr 22 '15 at 07:36
  • No need to remove manually. need to check the every array(words) with other arrays(words). need to remove 2nd words. eg) run and running are available in two arrays. the output should be run. – RSKMR Apr 22 '15 at 07:38
  • Ah, I seem to have missed the point of the question if it is about a specific algorithm – user3154108 Apr 22 '15 at 07:38
  • [This might helpful](http://programmers.stackexchange.com/questions/133778/how-can-i-extract-words-from-a-sentence-and-determine-what-part-of-speech-each-i) – ozil Apr 22 '15 at 07:49

2 Answers2

1

for the case mentioned by @Grimbode From OP

Check if a word is completely present in another word

So smile not completely present in smiling

So this solution might be helpful

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;
  }, []);
}

NOTE: Polyfill for old browser from 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>
Grundy
  • 13,060
  • 3
  • 33
  • 51
0

Check this. Although this will not work in the case mentioned by @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>
Hannan Hossain
  • 702
  • 1
  • 10
  • 23
  • Its looks good. but its possible to work in the case mentioned by @Grimbode ? – RSKMR Apr 22 '15 at 09:16
  • @RSKMR I think if you use Levenshtein distance algo then 'smiling' can be replaced by 'smile'. But I think this won't resolve your actual target."replace present continuous form to present form" For example Lavenshtein distance algorithm can't replace 'lying' to 'lie'. – Hannan Hossain Apr 22 '15 at 09:37