settingsAccountsettings
Menusettings

Q: Extract Unique Words - JavaScript task

+3 votes

Write a JS function that extracts all UNIQUE words from a valid text, and stores them. Ensure that there are NO duplicates in the stored words. Once you find a word, there is no need for you to store it again if you meet it again in the text. You also need to make all characters from the words you’ve stored – lowercase.

The input comes as array of strings. Each element will represent a sentence.

Examples:

Input:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis hendrerit dui.
Quisque fringilla est urna, vitae efficitur urna vestibulum fringilla.
Vestibulum dolor diam, dignissim quis varius non, fermentum non felis.
Vestibulum ultrices ex massa, sit amet faucibus nunc aliquam ut.
Morbi in ipsum varius, pharetra diam vel, mattis arcu.
Integer ac turpis commodo, varius nulla sed, elementum lectus.
Vivamus turpis dui, malesuada ac turpis dapibus, congue egestas metus.

Output:
lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit, pellentesque, quis, hendrerit, dui, quisque, fringilla, est, urna, vitae, efficitur, vestibulum, diam, dignissim, varius, non, fermentum, felis, ultrices, ex, massa, faucibus, nunc, aliquam, ut, morbi, in, pharetra, vel, mattis, arcu, integer, ac, turpis, commodo, nulla, sed, elementum, lectus, vivamus, malesuada, dapibus, congue, egestas, metus


Input:
Interdum et malesuada fames ac ante ipsum primis in faucibus.
Vestibulum volutpat lacinia blandit.
Pellentesque dignissim odio in hendrerit lacinia.
Vivamus placerat porttitor purus nec hendrerit.
Aliquam erat volutpat. Donec ac augue ligula.
Praesent venenatis sapien vitae libero ornare, nec pulvinar velit finibus.
Proin dui neque, rutrum vel dolor ut, placerat blandit sapien.
Pellentesque at est arcu.
Nullam eget orci laoreet, feugiat nisi vitae, egestas libero.
Pellentesque pulvinar aliquet felis.
Interdum et malesuada fames ac ante ipsum primis in faucibus.
Etiam sit amet nisl ex.
Sed lacinia pretium metus quis fermentum.
Praesent a ante suscipit, efficitur risus cursus, scelerisque risus.

Output:

interdum, et, malesuada, fames, ac, ante, ipsum, primis, in, faucibus, vestibulum, volutpat, lacinia, blandit, pellentesque, dignissim, odio, hendrerit, vivamus, placerat, porttitor, purus, nec, aliquam, erat, donec, augue, ligula, praesent, venenatis, sapien, vitae, libero, ornare, pulvinar, velit, finibus, proin, dui, neque, rutrum, vel, dolor, ut, at, est, arcu, nullam, eget, orci, laoreet, feugiat, nisi, egestas, aliquet, felis, etiam, sit, amet, nisl, ex, sed, pretium, metus, quis, fermentum, a, suscipit, efficitur, risus, cursus, scelerisque


The output is all of the unique words you’ve found, each with each, separated by a coma and a space, printed in the order in which you’ve found them.

asked in JavaScript category by user matthew44

1 Answer

+2 votes

Here is the code:

function extractUniqueWords(inputSentences) {
    let wordPattern = /\b[a-zA-Z0-9_]+\b/g;
    let words = new Set();
    for (let sentence of inputSentences) {
        let matches = sentence.match(wordPattern);
        matches.forEach((x=>words.add(x.toLowerCase())))
    }

    console.log(Array.from(words.values()).join(", "));
    //or//console.log([...words.values()].join(", "));
}

extractUniqueWords([
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit.Pellentesque quis hendrerit dui.",
        "Quisque fringilla est urna, vitae efficitur urna vestibulum fringilla.",
        "Vestibulum dolor diam, dignissim quis varius non, fermentum non felis.",
        "Vestibulum ultrices ex massa, sit amet faucibus nunc aliquam ut.",
        "Morbi in ipsum varius, pharetra diam vel, mattis arcu.",
        "Integer ac turpis commodo, varius nulla sed, elementum lectus.",
        "Vivamus turpis dui, malesuada ac turpis dapibus, congue egestas metus."
    ]
);

 

answered by user mitko
...