I have a problem, and can't figure out a soulution right now.
I have a string like this "abcd dfeg uyti lksh bxnm abcd uyti".
And i want to replace the duplicates with some other text (i use a function to generate random letters).
But there is a problem, that the new text is alerdy generated.
EXAMPLE INPUT "aaaa bbbb cccc bbbb cccc dddd eeee"
My output is like this "aaaa bbbb cccc aaaa eeee dddd eeee". which is wrong
But the output i want is like this "aaaa bbbb cccc ffff gggg dddd eeee"
I tried this code
function formatTEXT(text) {
let words = text.split(" ");
let replaced = new Set();
let duplicatesFound = true;
while (duplicatesFound) {
const wordCounts = {};
duplicatesFound = false;
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (!wordCounts[word]) {
wordCounts[word] = 1;
} else {
wordCounts[word]++;
}
}
for (let i = 0; i < words.length; i++) {
let replacement = generateTags(lettersN);
const word = words[i];
if (!replaced.has(word) && wordCounts[word] > 1 && !wordCounts[replacement]) {
words[i] = replacement;
replaced.add(word);
duplicatesFound = true;
console.log(replacement);
}
}
}
return words.join(" ");
}
formatTEXT("aaaa bbbb cccc bbbb cccc dddd eeee");
but it still does not work, there are still some duplicates in here.
uyti lksh bxnmit not helpful.!wordCounts[replacement]as condition this function's result strongly depends on whatgenerateTagsdoes. Without its implementation what do you expect us to say?"aa", repeated 27 times? Do you expect the random generator to replace the other 26 instances with"bb"or attempt to cycle through all lowercase letters? If the latter case is expected, what should the last instance be replaced with, since all lowercase letters would be exhausted at that point?