Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Duplicate Encoder JavaScript, try to use indexOf but the output is still incorect

convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once. can't find where i make a mistake

  1. "din" => "((("

  2. "recede" => "()()()"

  3. "Success" => ")())())"

  4. "(( @" => "))(("

    const duplicateEncode = (word) => {
     let newString = ''; 
      [...word.toLowerCase()].filter((e, i) => {
       if (word.indexOf(e) !== i) {
          newString += ')';
        } else if (word.lastIndexOf(e) !== i ) {
          newString += ')';
        } else newString += '(';
      });
    return newString;
    }
    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You issue seems to be that you're using .indexOf() and .lastIndexOf() on word, which contains both uppercase and lowercase letters, but e will always be the lowercase character from your input, which results in .indexOf() and .lastIndexOf() not being able to find the letter when it's upercase. Instead, store the lowercase version of your input in a new variable and use that when you call .indexOf()/.lastIndexOf():

const duplicateEncode = (word) => {
  let newString = '';
  const lowerWord = word.toLowerCase();
  [...lowerWord].forEach((e, i) => {
   if (lowerWord.indexOf(e) !== i) {
      newString += ')';
    } else if (lowerWord.lastIndexOf(e) !== i ) {
      newString += ')';
    } else newString += '(';
  });
  return newString;
}

console.log(duplicateEncode("din")); // "((("
console.log(duplicateEncode("recede")); // "()()()"
console.log(duplicateEncode("Success")); // ")())())"
console.log(duplicateEncode("(( @")); // "))(("
console.log(duplicateEncode("nGnnI)nPne@uwJ")); // ")())(()()((((("

You should also use .forEach() instead of .filter() as you're not filtering and using the array that .filter() returns.

Here is another appraoch that involes creating a Map (similar to an object), that holds the frequency for each char as a value. Using .replace() we can return a new string where we replace each character based on if it appears multiple times or not:

const duplicateEncode = (word) => {
  const lower = word.toLowerCase();
  const charFreq = [...lower].reduce((map, c) => map.set(c, (map.get(c) ?? 0) + 1), new Map);
  return lower.replace(/./ug, (c) => charFreq.get(c) === 1 ? "(" : ")");
}


console.log(duplicateEncode("din")); // "((("
console.log(duplicateEncode("recede")); // "()()()"
console.log(duplicateEncode("Success")); // ")())())"
console.log(duplicateEncode("(( @")); // "))(("
console.log(duplicateEncode("nGnnI)nPne@uwJ")); // ")())(()()((((("

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!