Is there a way to eliminate specific words that are repeated exactly twice in a string, and if possible, add the word "and"? This should only apply to certain characters.
For instance, take the following example: "Intruction in seated LE AROM x10 each instruction in standing LE AROM x10 each."
The desired outcome is: "Instruction in seated LE AROM and standing LE AROM x10 each".
I attempted various methods such as looping through the string and removing duplicates, but it caused me to lose needed information. I also experimented with a solution from another individual's query, although they were focused on counting occurrences of a word rather than my particular issue.
function removeDuplicate(str) {
let words = JSON.stringify(str).toLowerCase().split(' ')
let wordsCount = {}
words.forEach(word => {
wordsCount[word] = (wordsCount[word] || 0) + 1
})
}