Remove duplicate letters from a string

I’m really struggling with this practice problem and could really use some help. Below are the requirements. So far I’ve come up with code that loops through the given string and saves it to an array, but after that I have no idea how to go about removing duplicate letters from the final array. Any help is much appreciated.

// Write an application that takes a single string as input.
// Write a function removeDuplicates that removes duplicate letters, case-insensitively, so
// that every letter appears once and only once.
// Always keep the first occurrence of a letter, regardless of case.
// The function should only accept uppercase and lowercase letters.
// Your application should output the result of this function when called on the input string to the console.
// Example: the output from an input String of “AbraCadABraAlakAzam” is shown below.
// AbrCdlkzm

Below is the code I have so far.

let arr1 = [];
let finalArr = [];

const removeDuplicates = (input) => {
    for (let inputIndex = 0; inputIndex < input.length; inputIndex++) {
    //not sure at all about this next part
    for (let j = 0; j < arr1.length; j++) {
      if (arr1[j] === arr1[j]) {


1 Like

The above code changes the order of the string so the output cannot be mapped over the original. See if you can sort out your algorithm to retain original order.

1 Like

I see. For some reason I was thinking that .sort() would help to better compare and find duplicates. I’ve removed that method.


Some methods to consider implementing…


Surely that belongs inside the function. Think about what happens if you call the function multiple times, there would be junk in them from previous runs. There’s no reason for them to exist outside the function either.

As for figuring out what you need to do, do it manually with pen and paper or in your head. Observe what you end up doing. There’s probably no reason why your program would do it any differently. Or, in other words, you already know what you should do.
You might not immediately know how to write those things, but you certainly do need to decide what those things are, after which you can consider how to implement them.

1 Like

So very true, and intuitive.