0

Given an array:

let myArr = ['Maybe he is a student', 'He is a boy', 'a boy'];

A javascript code is needed to remove all words in each element which are present in ALL of the other elements of the array and be unique so that no elements are repeated in the results., so the wanted result would be:

return ['Maybe he is student','He is boy', 'boy']; // "a" is common thus removed

Any suggestion on an efficient solution? thx

edit
My options are:
1) convert each element to an array and use some underscore magic.
2) concat 2 elements at a time and remove duplicate words.
3) loop with in a loop and pull my hair...

14
  • 1
    I think that the description is wrong. The result should be just "Maybe student". Commented Apr 28, 2017 at 0:27
  • @Kinduser and student? Commented Apr 28, 2017 at 0:28
  • 2
    This seems like a homework assignment. @Fred J. how would you start tackling this problem? Commented Apr 28, 2017 at 0:31
  • Tried something yet? Commented Apr 28, 2017 at 0:31
  • 1
    Your expected results are perhaps incorrect ... "boy" appears twice so should not be included. Maybe I'm reading it wrong Commented Apr 28, 2017 at 1:13

2 Answers 2

3

Maybe there is a way to do this without iterating through the array twice, but if there is, it's beyond me. This seems to be adequate:

var myArr = ['Maybe he is a student', 'He is a boy', 'a boy', 'boy boy'];
var count = {};
for (let sentence of myArr) {
    var current = new Set(); // to keep track of duplicates in the current sentence
    for(let word of sentence.split(" ").map( x => x.toLowerCase() )) {
        if (!current.has(word)) {
            count[word] = ++count[word] || 1;
            current.add(word);
        }
    }
}
var second = [];
for (let sentence of myArr) {
    partial = sentence.split(" ").filter( x => count[x.toLowerCase()] != myArr.length );
    if (0 != partial.length) second.push(partial.join(" "));
}
console.log(second.join(", "))

Sign up to request clarification or add additional context in comments.

5 Comments

This is not a homework but sure I made it look like one ;), To make up an array of things I have seen before is another task given that the array elements are not known in advance. So I have to loop over each word of each array element and check condition of words equality. If that is the case, then no need to filter and set since I can just remove the word right there and then.
Ok ... so, trusting you here that I'm not doing your homework... lol
@FredJ.is this supposed to be case sensitive?
No case sensitive.
Strikes me that this has a bug.... if one of the sentences has the same word twice, this will be inaccurate (fixed)
0

Might not be the optimal solution. But this will do the job.

let myArr = ['Maybe he is a student', 'He is a boy', 'a boy'];
    
const t = myArr.reduce(function(a, c){
  a = a || {};
  c.split(' ').forEach(function(i){
    a[i.toLowerCase()] = a[i.toLowerCase()] ? a[i.toLowerCase()] + 1 : 1
  });
  return a;
}, [])
    
var result  = myArr.map(function(text){
  const arr =  text.split(' ');
  const r = arr.filter(function(item) { return t[item.toLowerCase()]===1 })
  return r.join(' ')
}).filter(function(y){ return y });
    
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.