Remove Duplicates

Problem

Write a program that removes duplicate characters and outputs when a single lowercase character string is input. Each character in the stripped string retains the order of the original string.

input

A string is entered on the first line.

ksekkset

output

Prints a string with duplicate characters removed on the first line.

kset

Solution

using new Set()

function solution(s) {
	let answer = '';
	new Set(s).forEach((el) => (answer += el));
	return answer;
}

using indexOf()

function solution(s) {
	let answer = '';

	for (let i = 0; i < s.length; i++) {
		// if s[i] is an only number, it should be i === s.indexOf(s[i])
		// console.log(s[i], i, s.indexOf(s[i]));

		if (s.indexOf(s[i]) === i) answer += s[i];
	}

	return answer;
}

Problem

input

ksekkset, k

output

3
function solution(s, v) {
	let answer = 0;
	let pos = s.indexOf(v);

	while (pos !== -1) {
		answer++;
		pos = s.indexOf(v, pos + 1);
	}

	return answer;
}