Given a string, for example: data
, I am looking to extract all new characters and store them in an array. Following the provided example, the desired output should be:
characters = ["d","a","t"];
My current approach is as follows:
const balanced = string => {
var characters = [];
var characters_Counter = 0;
for (var i = 0; i < string.length; i++) {
if (!characters.includes(string.charAt(i))){
characters[characters_Counter] = string.charAt(i);
characters_Counter++;
}
console.log(characters[i]);
}
};
Within the if
statement, the goal is to compare each character of string
with all the elements inside characters[]
. If it is a new character, then perform the actions specified within the if
block. However, I am currently facing difficulties in implementing this comparison effectively.