Skip to content

Instantly share code, notes, and snippets.

@jchandra74
Created February 7, 2020 17:04
Show Gist options
  • Save jchandra74/bedbb8778582bcfd7340182c7398484c to your computer and use it in GitHub Desktop.
Save jchandra74/bedbb8778582bcfd7340182c7398484c to your computer and use it in GitHub Desktop.
JS Bin Implementation of Find First Non Repeatable Character in a String // source https://jsbin.com/gomukah
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Implementation of Find First Non Repeatable Character in a String">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
console.clear();
var sut = findFirstNonRepeatableCharIn;
testEqual('', undefined, sut);
testEqual('d', 'd', sut);
testEqual('dd', undefined, sut);
testEqual('dda', 'a', sut);
testEqual('ddac', 'a', sut);
testEqual('ddacc', 'a', sut);
testEqual('ddaacc', undefined, sut);
testEqual('aaaabbbcddddeeef', 'c', sut);
function findFirstNonRepeatableCharIn(input) {
/*
Reason of why I am implementing it this way is because
I want to stop processing right when the character is found for efficiency.
*/
// Handle the special cases
if (!input)
return undefined;
if (input.length === 1)
return input[0];
var previous = input[0];
var repeated = false;
var current;
for (var i = 1; i < input.length; i++) {
current = input[i];
if (current === previous && !repeated) {
repeated = true;
/*
We could do the following instruction,
but since it's already the same, it's pointless and redundant.
Skipping it will safe some execution time.
*/
//previous = current;
/*
I prefer to start the next iteration immediately here.
It shows the intent clearly and remove any ambiguity.
*/
continue;
}
// Handle character changes
if (current !== previous) {
// Found it
if (!repeated)
return previous;
// Handle not found
repeated = false;
}
previous = current;
}
/*
Finally, handle special cases when we have iterated over the
entire input string.
*/
return !repeated ? current : undefined;
}
function testEqual(input, expected, sut) {
var actual = sut(input);
var info = "Input: '" + input + "' Expected: " + expected + " Actual: " + actual;
if (actual === expected)
console.info("PASS " + info);
else
console.error("FAIL " + info);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoianNiaW4uanMiLCJzb3VyY2VSb290Ijo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oZSBmb2xsb3dpbmcgaGVyZSxcbiAgICAgICAgYnV0IGRlY2lkZWQgbm90IHRvIGRvIGl0IHNpbmNlIGl0IHZlcnkgY2xvc2UgdG8gdGhlIGVuZCBvZlxuICAgICAgICB0aGUgaXRlcmF0aW9uIGFuZCBub3QgZG9pbmcgaXQgYmFzaWNhbGx5IHNhdmUgc29tZSB2ZXJib3NpdHlcbiAgICAgICAgaW4gdGhlIGNvZGUgc28gSSBhbSBsZXR0aW5nIHRoZSByZWd1bGFyIGZsb3cgdG8gZG8gaXQgZm9yIG1lLlxuICAgICAgKi9cblxuICAgICAgLy8gcHJldmlvdXMgPSBjdXJyZW50O1xuICAgICAgLy8gY29udGludWU7XG4gICAgfVxuXG4gICAgcHJldmlvdXMgPSBjdXJyZW50O1xuICB9XG5cbiAgLypcbiAgICBGaW5hbGx5LCBoYW5kbGUgc3BlY2lhbCBjYXNlcyB3aGVuIHdlIGhhdmUgaXRlcmF0ZWQgb3ZlciB0aGVcbiAgICBlbnRpcmUgaW5wdXQgc3RyaW5nLlxuICAqL1xuICByZXR1cm4gIXJlcGVhdGVkID8gY3VycmVudCA6IHVuZGVmaW5lZDtcbn1cblxuZnVuY3Rpb24gdGVzdEVxdWFsKGlucHV0LCBleHBlY3RlZCwgc3V0KSB7XG4gIGNvbnN0IGFjdHVhbCA9IHN1dChpbnB1dCk7XG4gIGNvbnN0IGluZm8gPSBgSW5wdXQ6ICcke2lucHV0fScgRXhwZWN0ZWQ6ICR7ZXhwZWN0ZWR9IEFjdHVhbDogJHthY3R1YWx9YDtcblxuICBpZiAoYWN0dWFsID09PSBleHBlY3RlZClcbiAgICBjb25zb2xlLmluZm8oYFBBU1MgJHtpbmZvfWApO1xuICBlbHNlXG4gICAgY29uc29sZS5lcnJvcihgRkFJTCAke2luZm99YCk7XG59Il19
</script>
<script id="jsbin-source-javascript" type="text/javascript">console.clear();
const sut = findFirstNonRepeatableCharIn;
testEqual('', undefined, sut);
testEqual('d', 'd', sut);
testEqual('dd', undefined, sut);
testEqual('dda', 'a', sut);
testEqual('ddac', 'a', sut);
testEqual('ddacc', 'a', sut);
testEqual('ddaacc', undefined, sut);
testEqual('aaaabbbcddddeeef', 'c', sut);
function findFirstNonRepeatableCharIn(input) {
/*
Reason of why I am implementing it this way is because
I want to stop processing right when the character is found for efficiency.
*/
// Handle the special cases
if (!input) return undefined;
if (input.length === 1) return input[0];
var previous = input[0];
var repeated = false;
var current;
for (var i = 1; i < input.length; i++) {
current = input[i];
if (current === previous && !repeated) {
repeated = true;
/*
We could do the following instruction,
but since it's already the same, it's pointless and redundant.
Skipping it will safe some execution time.
*/
//previous = current;
/*
I prefer to start the next iteration immediately here.
It shows the intent clearly and remove any ambiguity.
*/
continue;
}
// Handle character changes
if (current !== previous) {
// Found it
if (!repeated) return previous;
// Handle not found
repeated = false;
/*
I was considering to do the following here,
but decided not to do it since it very close to the end of
the iteration and not doing it basically save some verbosity
in the code so I am letting the regular flow to do it for me.
*/
// previous = current;
// continue;
}
previous = current;
}
/*
Finally, handle special cases when we have iterated over the
entire input string.
*/
return !repeated ? current : undefined;
}
function testEqual(input, expected, sut) {
const actual = sut(input);
const info = `Input: '${input}' Expected: ${expected} Actual: ${actual}`;
if (actual === expected)
console.info(`PASS ${info}`);
else
console.error(`FAIL ${info}`);
}</script></body>
</html>
"use strict";
console.clear();
var sut = findFirstNonRepeatableCharIn;
testEqual('', undefined, sut);
testEqual('d', 'd', sut);
testEqual('dd', undefined, sut);
testEqual('dda', 'a', sut);
testEqual('ddac', 'a', sut);
testEqual('ddacc', 'a', sut);
testEqual('ddaacc', undefined, sut);
testEqual('aaaabbbcddddeeef', 'c', sut);
function findFirstNonRepeatableCharIn(input) {
/*
Reason of why I am implementing it this way is because
I want to stop processing right when the character is found for efficiency.
*/
// Handle the special cases
if (!input)
return undefined;
if (input.length === 1)
return input[0];
var previous = input[0];
var repeated = false;
var current;
for (var i = 1; i < input.length; i++) {
current = input[i];
if (current === previous && !repeated) {
repeated = true;
/*
We could do the following instruction,
but since it's already the same, it's pointless and redundant.
Skipping it will safe some execution time.
*/
//previous = current;
/*
I prefer to start the next iteration immediately here.
It shows the intent clearly and remove any ambiguity.
*/
continue;
}
// Handle character changes
if (current !== previous) {
// Found it
if (!repeated)
return previous;
// Handle not found
repeated = false;
}
previous = current;
}
/*
Finally, handle special cases when we have iterated over the
entire input string.
*/
return !repeated ? current : undefined;
}
function testEqual(input, expected, sut) {
var actual = sut(input);
var info = "Input: '" + input + "' Expected: " + expected + " Actual: " + actual;
if (actual === expected)
console.info("PASS " + info);
else
console.error("FAIL " + info);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoianNiaW4uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJqc2Jpbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsT0FBTyxDQUFDLEtBQUssRUFBRSxDQUFDO0FBQ2hCLElBQU0sR0FBRyxHQUFHLDRCQUE0QixDQUFDO0FBQ3pDLFNBQVMsQ0FBQyxFQUFFLEVBQUUsU0FBUyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQzlCLFNBQVMsQ0FBQyxHQUFHLEVBQUUsR0FBRyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQ3pCLFNBQVMsQ0FBQyxJQUFJLEVBQUUsU0FBUyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQ2hDLFNBQVMsQ0FBQyxLQUFLLEVBQUUsR0FBRyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQzNCLFNBQVMsQ0FBQyxNQUFNLEVBQUUsR0FBRyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQzVCLFNBQVMsQ0FBQyxPQUFPLEVBQUUsR0FBRyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQzdCLFNBQVMsQ0FBQyxRQUFRLEVBQUUsU0FBUyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0FBQ3BD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3RlZCwgc3V0KSB7XG4gIGNvbnN0IGFjdHVhbCA9IHN1dChpbnB1dCk7XG4gIGNvbnN0IGluZm8gPSBgSW5wdXQ6ICcke2lucHV0fScgRXhwZWN0ZWQ6ICR7ZXhwZWN0ZWR9IEFjdHVhbDogJHthY3R1YWx9YDtcblxuICBpZiAoYWN0dWFsID09PSBleHBlY3RlZClcbiAgICBjb25zb2xlLmluZm8oYFBBU1MgJHtpbmZvfWApO1xuICBlbHNlXG4gICAgY29uc29sZS5lcnJvcihgRkFJTCAke2luZm99YCk7XG59Il19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment