Check for repeated characters in a string Javascript - javascript

I was wondering if there is a way to check for repeated characters in a string without using double loop. Can this be done with recursion?
An example of the code using double loop (return true or false based on if there are repeated characters in a string):
var charRepeats = function(str) {
for(var i = 0; i <= str.length; i++) {
for(var j = i+1; j <= str.length; j++) {
if(str[j] == str[i]) {
return false;
}
}
}
return true;
}
Many thanks in advance!

This will do:
function hasRepeats (str) {
return /(.).*\1/.test(str);
}

(A recursive solution can be found at the end of this answer)
You could simply use the builtin javascript Array functions some MDN some reference
var text = "test".split("");
text.some(function(v,i,a){
return a.lastIndexOf(v)!=i;
});
callback parameters:
v ... current value of the iteration
i ... current index of the iteration
a ... array being iterated
.split("") create an array from a string
.some(function(v,i,a){ ... }) goes through an array until the function returns true, and ends than right away. (it doesn't loop through the whole array, which is good for performance)
Details to the some function here in the documentation
Here some tests, with several different strings:
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var text = texts[idx].split("");
document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");
}
//tested on win7 in chrome 46+
If you will want recursion.
Update for recursion:
//recursive function
function checkString(text,index){
if((text.length - index)==0 ){ //stop condition
return false;
}else{
return checkString(text,index + 1)
|| text.substr(0, index).indexOf(text[index])!=-1;
}
}
// example Data to test
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var txt = texts[idx];
document.write( txt + " ->" + checkString(txt,0) + "<br/>");
}
//tested on win7 in chrome 46+

you can use .indexOf() and .lastIndexOf() to determine if an index is repeated. Meaning, if the first occurrence of the character is also the last occurrence, then you know it doesn't repeat. If not true, then it does repeat.
var example = 'hello';
var charRepeats = function(str) {
for (var i=0; i<str.length; i++) {
if ( str.indexOf(str[i]) !== str.lastIndexOf(str[i]) ) {
return false; // repeats
}
}
return true;
}
console.log( charRepeats(example) ); // 'false', because when it hits 'l', the indexOf and lastIndexOf are not the same.

function chkRepeat(word) {
var wordLower = word.toLowerCase();
var wordSet = new Set(wordLower);
var lenWord = wordLower.length;
var lenWordSet =wordSet.size;
if (lenWord === lenWordSet) {
return "false"
} else {
return'true'
}
}

Using regex to solve=>
function isIsogram(str){
return !/(\w).*\1/i.test(str);
}
console.log(isIsogram("isogram"), true );
console.log(isIsogram("aba"), false, "same chars may not be adjacent" );
console.log(isIsogram("moOse"), false, "same chars may not be same case" );
console.log(isIsogram("isIsogram"), false );
console.log(isIsogram(""), true, "an empty string is a valid isogram" );

The algorithm presented has a complexity of (1 + n - (1)) + (1 + n - (2)) + (1 + n - (3)) + ... + (1 + n - (n-1)) = (n-1)*(1 + n) - (n)(n-1)/2 = (n^2 + n - 2)/2 which is O(n2).
So it would be better to use an object to map and remember the characters to check for uniqueness or duplicates. Assuming a maximum data size for each character, this process will be an O(n) algorithm.
function charUnique(s) {
var r = {}, i, x;
for (i=0; i<s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
On a tiny test case, the function indeed runs a few times faster.
Note that JavaScript strings are defined as sequences of 16-bit unsigned integer values. http://bclary.com/2004/11/07/#a-4.3.16
Hence, we can still implement the same basic algorithm but using a much quicker array lookup rather than an object lookup. The result is approximately 100 times faster now.
var charRepeats = function(str) {
for (var i = 0; i <= str.length; i++) {
for (var j = i + 1; j <= str.length; j++) {
if (str[j] == str[i]) {
return false;
}
}
}
return true;
}
function charUnique(s) {
var r = {},
i, x;
for (i = 0; i < s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charUnique2(s) {
var r = {},
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charCodeUnique(s) {
var r = [],
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s.charCodeAt(i);
if (r[x])
return false;
r[x] = true;
}
return true;
}
function regExpWay(s) {
return /(.).*\1/.test(s);
}
function timer(f) {
var i;
var t0;
var string = [];
for (i = 32; i < 127; i++)
string[string.length] = String.fromCharCode(i);
string = string.join('');
t0 = new Date();
for (i = 0; i < 10000; i++)
f(string);
return (new Date()) - t0;
}
document.write('O(n^2) = ',
timer(charRepeats), ';<br>O(n) = ',
timer(charUnique), ';<br>optimized O(n) = ',
timer(charUnique2), ';<br>more optimized O(n) = ',
timer(charCodeUnique), ';<br>regular expression way = ',
timer(regExpWay));

let myString = "Haammmzzzaaa";
myString = myString
.split("")
.filter((item, index, array) => array.indexOf(item) === index)
.join("");
console.log(myString); // "Hamza"

Another way of doing it using lodash
var _ = require("lodash");
var inputString = "HelLoo world!"
var checkRepeatition = function(inputString) {
let unique = _.uniq(inputString).join('');
if(inputString.length !== unique.length) {
return true; //duplicate characters present!
}
return false;
};
console.log(checkRepeatition(inputString.toLowerCase()));

const str = "afewreociwddwjej";
const repeatedChar=(str)=>{
const result = [];
const strArr = str.toLowerCase().split("").sort().join("").match(/(.)\1+/g);
if (strArr != null) {
strArr.forEach((elem) => {
result.push(elem[0]);
});
}
return result;
}
console.log(...repeatedChar(str));
You can also use the following code to find the repeated character in a string

//Finds character which are repeating in a string
var sample = "success";
function repeatFinder(str) {
let repeat="";
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str.charAt(i) == str.charAt(j) && repeat.indexOf(str.charAt(j)) == -1) {
repeat += str.charAt(i);
}
}
}
return repeat;
}
console.log(repeatFinder(sample)); //output: sc

const checkRepeats = (str: string) => {
const arr = str.split('')
const obj: any = {}
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) {
return true
}
obj[arr[i]] = true
}
return false
}
console.log(checkRepeats('abcdea'))

function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = '
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = 'haiiiiiiiiii'
console.log(repeat(str))
'
console.log(repeat(str))

Cleanest way for me:
Convert the string to an array
Make a set from the array
Compare the length of the set and the array
Example function:
function checkDuplicates(str) {
const strArray = str.split('');
if (strArray.length !== new Set(strArray).size) {
return true;
}
return false;
}

You can use "Set object"!
The Set object lets you store unique values of any type, whether
primitive values or object references. It has some methods to add or to check if a property exist in the object.
Read more about Sets at MDN
Here how i use it:
function isIsogram(str){
let obj = new Set();
for(let i = 0; i < str.length; i++){
if(obj.has(str[i])){
return false
}else{
obj.add(str[i])
}
}
return true
}
isIsogram("Dermatoglyphics") // true
isIsogram("aba")// false

Related

JS - getting the longest non-repeating character substring from a string

I am trying to solve a task on the leetcode site. My approach to solving this problem was to iterate over a string, and then fill first one array until I find a repeating character, and then start filling another array, until I find a repeating character there as well. Once I would have two filled arrays, I would compare them and keep the longer one, until I am done with iteration. But with these approach I end up checking a lot of things and not sure what to do when I have the secondArray filled and I am still doing the subiteration, to avoid filling the firstArray as well.
In general this code is to cumbersome, and I am not sure anymore how should I solve this?
s = "anviaj"
s = "eeydgwdykpv"
var lengthOfLongestSubstring = function(s) {
let firstArray = [];
let secondArray = [];
let firstArrayFull = false;
let secondArrayFull = false;
let subIterationFinished = false;
if (s.length == 1)
return s;
for (var i = 0; i < s.length -1; i++) {
if (!firstArrayFull) {
firstArray.push(s.charAt(i));
} else if (!secondArrayFull) { secondArray.push(s.charAt(i)); }
for (let j = i + 1; j < s.length; j++) {
if (j + 1 == s.length) {
subIterationFinished = true;
checkArrays();
}
if (!firstArrayFull && !firstArray.includes(s.charAt(j))) {
if (secondArrayFull && !subIterationFinished) {
firstArray = firstArray;
} else {
firstArray.push(s.charAt(j));
}
} else {
firstArrayFull = true;
if (secondArrayFull) {
checkArrays();
}
}
if (firstArrayFull && secondArray.length != 0 && secondArray.includes(s.charAt(j))) {
secondArrayFull = true;
checkArrays();
}
if (firstArrayFull && secondArray.length != 0 && !secondArray.includes(s.charAt(j))) {
secondArray.push(s.charAt(j));
}
}
}
function checkArrays() {
if (secondArray.length > firstArray.length) {
firstArray = [];
firstArrayFull = false;
secondArrayFull = true;
} else {
secondArray = [];
secondArrayFull = false;
firstArrayFull = true;
}
}
return secondArray.length > firstArray.length ? secondArray : firstArray;
};
console.log(lengthOfLongestSubstring(s));
This is a valid, O(n) solution which only constructs the final string once, tested against a 20.000 length string (it should be still fast with a million characters, but stackoverflow limits input to 30K characters).
/**
* #param {string} s
* #return {number}
*/
var lengthOfLongestSubstring = function(s) {
var l = 0;
var r = 0;
var maxL = 0;
var maxR = 0;
var length = () => r - l;
var maxLength = () => maxR - maxL;
var keys = {};
while (r < s.length) {
if (s[r] in keys) {
var newL = keys[s[r]] + 1;
for (var i = l; i < newL; i++) {
delete keys[s[i]];
}
if (maxLength() < length()) {
maxL = l;
maxR = r;
}
l = newL;
}
keys[s[r]] = r;
r = r + 1;
}
if (maxLength() < length()) {
maxL = l;
maxR = r;
}
return maxLength();
};
console.log(lengthOfLongestSubstring("2sLbj8wBPPa5RYgNNwzuYQ7Pz3IehWrxOPiAxbGTw1wpEKyPnWLEuiEiW8vPFGENfjICBeNBeslmT8UZZD7AyUp1WKrC8OneS7GzfacqpzzGB4awH4O7CosGQp3fe3UYTqPT44hHtYfGTLKaZkIt6dA1XQQCzvUYCQFFEGWxzPDFZt2D7a8XT4UEuBrUYo4ml4xds7cQflVD8Hyr9bJcCfC6BeUutQb4SrqUR3SlLNVMVlXEYsHLKbXjl0zYNFeIatfvx5w7UNEfLGqhtzNvGKVixQQ6OIIH835jo7IYp6G7FumW9asgKSjbbEQhfWKenep8yL3psmb1eUl2vWGxDKo8Z82UB4M5LcZ4o2W9nGHrpAkyOdJb6WIc3sfeThMAUHA7Ykh9bNmDrmW7J44eJ1XLbrN6xHnUC8LrGXYlFCh7x1bkjyy08Z7LVQGhnuDU1ZaovmKoTrtgPzvIEESakLXxB2t0RbrTVrnGcstUKtTyHqj8X6e3V0lqHjzGZ0Hpucp0cuJLSHOF4GTnju6JK74EbjN5Di9gy7qbe7K59mjdzlzsGHMRq9POl86bFUOgxyFmHWrPRkQXC1TS5YCUiy1Bbu6MXTLvbL3YnK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1jggYmwfCvkP7us3jjrnI3aO2xGXwjMbl7Qlc31aw8AUU2jmhXikBRR18IYHEZHCjiENhpUdD34yDFEt99Je2Gi5IYitzKvg6wHikxQxaCf7wZt5bJ0FEd8XA7FnlFS58x483d7ghi5xBvNfUtkRlwe4in6UcSRLWkrZsCYUfSjarAo2NL1guyuMbHB6jVHUhQBkUnFhKkZjMZ0csaZZocaR8B5FtQxcqzH9ZnEZFGpoKDu0TEB5mHuKQquJpQtUlpdx4qd0FrVNfawh7PW3CCbKCxuEj03PCnW3NzAgS9FPlMl5kZtOMx2T59SRw9qnbCs37ghJtMYgI1iaEq4ILNnVrDZ2OMFO3Pmp1iDLuudI9ubvi7HVclBfz0i8z5CJmbNN1GRsXtsUFxqauyAeO27ECVHHiJBCpoWmVh4FcsGcFD8ISFcPDQCADNomO5htszRRQRrxKYk7KimjiDh0B3NdEP6H7fttigwfTWvjhYBEB8fnET9wqCAyMonXaaPkq73N7TUdlFSsxCdNkixI64ksu4go1gdB4YxZ9ALoloPc2vUpjn18EX3x0EVabXKEHWNIgyfcFdbrjU6E3cQ6udgfH8AtWj8yhxwgK4P0fUqqJg3le0MPo5KybEWuTtoBsdcQ4WFYxuUcTAXF2hwdTwMhoST9OwKL4K4eRuA1g4SQqesLZmMFwXsCFKHRSO8bv48qplLX5QnGN7qm85OygqsV5NsxTpLCG8QSk7ribN32iUfkdPc53PTmSDL8HJGzVh5jaCE14StYPBd8g1uDK3SM5F2RQPV6pzUQDxhgAMJFv5Zp7bQ4SrqCfZxiCp1prx408tK3icnp6GyZCM2nFuLopKHE6X8LJB44Z8XjXBipeM5rQClrhpxb0TBTkmhWdEer1pAkTnMHX4PLCeowMaz5Q2KG7cEhY8VJjOhK22Ln"))
Use two variable, one to keep track of the longest substring and the other to keep track of current longest substring without repetition.
Iterate through your string.
Check if the current word contains the current character.
If it contains, then checks the length of current word with the longest substring. If it is greater, the update the longest substring.
Update the current word from the last seen index till the length of the current word.
Continue till end of your input string.
var lengthOfLongestSubstring = function(s) {
var longestSubstring = '';
var currentWord = '';
for(var i = 0 ; i < s.length ; i++){
var foundIndex = currentWord.indexOf(s[i]);
if(foundIndex > -1){
if(longestSubstring.length < currentWord.length)
longestSubstring = currentWord;
currentWord = currentWord.slice(foundIndex+1);
}
currentWord += s[i];
}
return longestSubstring.length < currentWord.length ? currentWord.length : longestSubstring.length;
};
console.log(lengthOfLongestSubstring("bbbbb"));
console.log(lengthOfLongestSubstring("abcdaajkljl"));
console.log(lengthOfLongestSubstring("pwwkew"));
console.log(lengthOfLongestSubstring("ysbghjjrfumqjpyktddsnxftvdqgxzlvrneaynufhgyqxwaqzelmbsiyxaubrqvguvehpmrykhvikkqzwttg"));
well this is challenging but i figure it out
here is an O(n) solution. cant do better then this
function lengthOfLongestSubstring(str) {
var currentLength = 0;
var result = 0;
var keys = {};
for (var i in str) {
if (keys[str[i]] === true) {
currentLength = 0;
keys = {};
}
keys[str[i]] = true;
currentLength++;
if (currentLength > result) {
result = currentLength;
}
}
return result
}
console.log(lengthOfLongestSubstring("bbbbb"));
console.log(lengthOfLongestSubstring("abcdaajkljl"));
console.log(lengthOfLongestSubstring("pwwkew"));
console.log(lengthOfLongestSubstring("ysbghjjrfumqjpyktddsnxftvdqgxzlvrneaynufhgyqxwaqzelmbsiyxaubrqvguvehpmrykhvikkqzwttg"));
Can use String#includes() to check if your substring has duplicates
s = "anviaj"
s1 = "eeydgwdykpv"
function findLongest(str) {
// reduce() iterates array created from split input and returns longest sub string
return str.split('').reduce((a,c,i)=>{
// start our substring using first character of str
let sub = str[0], j = 0;
// sub.includes(char) returns true if char already in sub string
while (++j < str.length && !sub.includes(str[j])) {
// no duplicate so add char to substring
sub += str[j];
}
// remove first character from input string each iteration
str = str.slice(1);
// replace current longest if applicable
return sub.length > a.length ? sub : a;
},'');
}
console.log(findLongest(s))
console.log(findLongest(s1))

Find missing letter in list of alphabets

I am trying to solve the following issue:
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
the inputs that I will get as strings are:
abce (Which should return d)
bcd (which should return undefined)
abcdefghjklmno (which should return i)
yz (which should return undefined)
my code currently looks like this:
function fearNotLetter(str) {
//create alphabet string
//find starting letter in alphabet str, using str
//compare letters sequentially
//if the sequence doesn't match at one point then return letter
//if all letters in str appear then return undefined
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i<alphabet.length && j<str.length) {
i++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j)) {
i++;
j++;
}
else if (alphabet.charCodeAt(i) !== str.charCodeAt(j)) {
i++;
j++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j-1)) {
return alphabet.charCodeAt(i-1);
}
}
}
}
fearNotLetter('abce');
Thanks for your help as always!
I would do it like this:
function fearNotLetter(str) {
var i, j = 0, m = 122;
if (str) {
i = str.charCodeAt(0);
while (i <= m && j < str.length) {
if (String.fromCharCode(i) !== str.charAt(j)) {
return String.fromCharCode(i);
}
i++; j++;
}
}
return undefined;
}
console.log(fearNotLetter('abce')); // "d"
console.log(fearNotLetter('bcd')); // undefined
console.log(fearNotLetter('bcdefh')); // "g"
console.log(fearNotLetter('')); // undefined
console.log(fearNotLetter('abcde')); // undefined
console.log(fearNotLetter('abcdefghjkl')); // "i"
i can go from 97 to 122, this interval corresponds to the ASCII codes of the lower case alphabet.
If you want it not to be case sensitive, just do str = str.toLowerCase() at the beginning of the function.
I think this is the simplest code to do this:
function skippedLetter(str) {
for (var i = 0; i < str.length - 1; i++) {
if (str.charCodeAt(i + 1) - str.charCodeAt(i) != 1) {
return String.fromCharCode(str.charCodeAt(i) + 1);
}
}
}
alert(skippedLetter('abce'));
This version will reject illegal input, accept both upper and lower case, check that there is only 1 hole in the range, and that there is exactly 1 character missing.
function skippedLetter(str) {
if (!str.match(/^[a-zA-Z]+$/)) return;
var letter = "", offset = str.charCodeAt(0);
for (var i = 1; i < str.length; i++) {
var diff = str.charCodeAt(i) - i - offset;
if (diff == 1) letter += String.fromCharCode(i + offset++)
else if (diff) return;
}
if (letter.length == 1) return letter;
}
alert(skippedLetter('123567')); // illegal characters
alert(skippedLetter('')); // empty string
alert(skippedLetter('a')); // too short
alert(skippedLetter('bc')); // nothing missing
alert(skippedLetter('df')); // skipped letter = e
alert(skippedLetter('GHIKLM')); // skipped letter = J
alert(skippedLetter('nOpRsT')); // cases mixed
alert(skippedLetter('nopxyz')); // too many characters missing
alert(skippedLetter('abcefgijk')); // character missing more than once
alert(skippedLetter('abcefgfe')); // out of order
Note that you have a typo in alphabet: There are two "e"s.
You could split the string into an array, then use the some method to short-circuit the loop when you don't find a match:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
missing,
i= 0;
str.split('').some(function(l1) {
var l2= alphabet.substr(i++, 1);
if(l1 !== l2) {
if(i===1) missing= undefined;
else missing= l2;
return true;
}
});
return missing;
}
console.log(fearNotLetter('abce')); //d
console.log(fearNotLetter('bcd')); //undefined
console.log(fearNotLetter('abcdefghjklmno')); //i
console.log(fearNotLetter('yz')); //undefined
Another function that may help:
var alphabet = "abcdefgheijklmnopqrstuvwxyz";
function fearNotLetter(a) {
function letterIndex(text, index) {
var letter = text.charAt(0);
if (alphabet.indexOf(letter) !== index) { return alphabet.charAt(index); } else { return letterIndex(text.substring(1), index + 1) }
}
if (alphabet.indexOf(a) === -1) {
return letterIndex(a, alphabet.indexOf(a.charAt(0)));
}
return undefined;
}
fearNotLetter("abc"); //Undefined
fearNotLetter("abce"); //d
fearNotLetter("fgi"); //h
This will do what you're looking for:
Hit run and check your console
function missingLetter (str) {
var alphabet = ("abcdefghijklmnopqrstuvwxyz");
var first = alphabet.indexOf(str[0]);
var strIndex = 0;
var missing;
for (var i = first ; i < str.length ; i++) {
if (str[strIndex] === alphabet[i]) {
strIndex++;
} else {
missing = alphabet[i];
}
}
return missing;
}
console.log(missingLetter("abce"));
console.log(missingLetter("bcd"));
console.log(missingLetter("abcdefghjklmno"));
console.log(missingLetter("yz"));
I think that you wanted to say that if a string doesn't start with "a" than return "undefined". So here's my code:
function fearNotLetter(str) {
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i < alphabet.length && j < str.length) {
if (alphabet.charAt(i) != str.charAt(j)) {
i++;
j++;
if (alphabet.charAt(i - 1) == "a") {
return "undefined";
} else {
return (alphabet.charAt(i - 1));
}
}
i++;
j++;
}
}
alert(fearNotLetter('abce'));
Here's the working JsFiddle.
You wanted the code to return the missing letter so I used CharAt.
You can make an array of letters and then search through it to see if it maches with letters from the string....
function fearNotLetter(str) {
var a = str.split('');
var array = [];
var j = 0;
for (var i = 1; i < a.length; i++) {
var d = a[i].charCodeAt(0);
var c = a[i - 1].charCodeAt(0);
var delta = d - c;
if (delta != 1) {
array[i] = String.fromCharCode(a[i - 1].charCodeAt(0) + 1);
}
}
str = array.join('');
if (str.length === 0) {
return undefined;
} else {
return str;
}
}
fearNotLetter('abcefr');
This is an even shorter answer thanks to RegExp() that allows you to create a Regular Expression on the fly and use match() to strip off the given letters from a generated String that has all the letters in the given range:
function fearNotLetter(str) {
var allChars = '';
var notChars = new RegExp('[^'+str+']','g');
for (var i=0;allChars[allChars.length-1] !== str[str.length-1] ;i++)
allChars += String.fromCharCode(str[0].charCodeAt(0)+i);
return allChars.match(notChars) ? allChars.match(notChars).join('') : undefined;
}
How about this one? it finds all missing letters anywhere between the first and the last given letters:
function fearNotLetter(str) {
var strArr = str.split('');
var missingChars = [], i = 0;
var nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
while (i<strArr.length - 1) {
if (nextChar !== strArr[i+1]){
missingChars.push(nextChar);
nextChar = String.fromCharCode(nextChar.charCodeAt(0)+1);
} else {
i++;
nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
}
}
return missingChars.join('') === '' ? undefined : missingChars.join('') ;
}
console.log(fearNotLetter("ab"));
Here is what I use:
function fearNotLetter(str) {
var firstLtrUnicode = str.charCodeAt(0),
lastLtrUnicode = str.charCodeAt(str.length - 1);
var holder = [];
for (var i=firstLtrUnicode; i<=lastLtrUnicode; i++) {
holder.push(String.fromCharCode(i));
}
var finalStr = holder.join('');
if ( finalStr === str ) { return undefined; }
else { return holder.filter( function(letter) {
return str.split('').indexOf(letter) === -1;
}).join(''); } }
Try this:
function fearNotLetter(str) {
var alp = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').split(''), i;
for (i = alp.indexOf(str.charAt(0)); i < str.length; i++) {
if (str.split('').indexOf(alp[i]) === -1) {
return alp[i];
}
}
return undefined;
}
fearNotLetter('bcd');
Here's my solution, which i find to be quite easy to interpret:
function fearNotLetter(str) {
var missingLetter;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
missingLetter = String.fromCharCode(str.charCodeAt(i) - 1);
}
}
return missingLetter;
}
I just did this challenge. I am a beginner to Javascript so this was my approach, very simple, sometimes you don't have to use the methods they provide but they also help. I hope you can understand it.
function fearNotLetter(str) {
var alphabet= "abcdefghijlmnopqrstuvwxyz";
var piece =alphabet.slice(0, str.length+1);
for(var i=0; i < piece.length; i++ ){
if(str.charCodeAt(0) != 97){
return undefined;
}
else if(str.indexOf(piece[i])===-1){
return piece[i];
}
}// for loop
}
fearNotLetter("abce");// It will return d
fearNotLetter("xy");//It will return undefined
fearNotLetter("bce");//It will return undefined
function fearNotLetter(str) {
//transform the string to an array of characters
str = str.split('');
//generate an array of letters from a to z
var lettersArr = genCharArray('a', 'z');
//transform the array of letters to string
lettersArr = lettersArr.join('');
//substr the a to z string starting from the first letter of the provided
string
lettersArr = lettersArr.substr(lettersArr.indexOf(str[0]), str.length);
//transform it again to an array of letters
lettersArr = lettersArr.split('');
//compare the provided str to the array of letters
for(var i=0; i<lettersArr.length;i++){
if(str[i] !== lettersArr[i])
return lettersArr[i];
}
return undefined;
}
function genCharArray(charA, charZ) {
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
for (; i <= j; ++i) {
a.push(String.fromCharCode(i));
}
return a;
}
fearNotLetter("bcd");
Here's my another answer in missing letters:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var first = alphabet.indexOf(str[0]);
var last = alphabet.indexOf(str[str.length-1]);
var alpha = alphabet.slice(first,last);
for(var i=0;i<str.length;i++){
if(str[i] !== alpha[i]){
return alpha[i];
}
}
}
console.log(fearNotLetter("abce"));
Overview:
const fearNotLetter = str => {
if(str.length < 26) {
return String.fromCharCode(
str.split("")
.map(x=> x.charCodeAt(0))
.sort((a,b) => a-b)
.find((x,i,a) => a[i+1]-x > 1) + 1
);
}
}
1 liner:
const fearNotLetter = str => str.length < 26 ? String.fromCharCode(str.split("").map(x=> x.charCodeAt(0)).sort((a,b) => a-b).find((x,i,a) => a[i+1]-x > 1) + 1) : undefined;
function fearNotLetter(str) {
var allChars = "";
var notChars = new RegExp("[^" + str + "]","g");
for (var i = 0; allChars[allChars.length - 1] !== str[str.length - 1]; i++)
allChars += String.fromCharCode(str[0].charCodeAt(0) + i);
return allChars.match(notChars) ? allChars.match(notChars).join("") : undefined;
}
function fearNotLetter(str) {
let alphabet = "abcdefghijklmnopqrstuvwxyz"
let startingPoint = alphabet.indexOf(str[0]);
let leaterNotMatch = alphabet.slice(startingPoint)
console.log(startingPoint)
for(let i = 0; i<str.length; i +=1){
if(str[i] != leaterNotMatch[i]){
return leaterNotMatch[i];
}
}
}
let result = fearNotLetter("abcdefghjklmno");
console.log(result)
function findMissingLetter(letters) {
const alphabet = ['A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
let otherAlphabet = [];
if (letters[0] !== letters[0].toUpperCase()) {
otherAlphabet = alphabet.map(item => item.toLowerCase());
} else {
otherAlphabet = alphabet;
}
const firstIndex = otherAlphabet.findIndex((aplhabetEl) => {
return aplhabetEl === letters[0];
});
const lastIndex = otherAlphabet.findIndex((aplhabetEl) => {
return aplhabetEl === letters[letters.length - 1];
});
const sliceAlphabet = otherAlphabet.slice(firstIndex, lastIndex + 1);
let result = '';
for (let i = 0; i < letters.length; i++) {
if (letters[i] !== sliceAlphabet[i]) {
result += sliceAlphabet[I];
}
}
return result.slice(0, 1);
}
You can also solve this question by using a map which we will call seen. For every letter, we will add it to our seen map. Then we will loop through the alphabet checking if we have come across that letter in our seen map.
If we have come across that letter in our seen map, then we will use a boolean flag foundFirstLetter and set that to true. Then inside another conditional foundFirstLetter, we can check if the letter is missing and if it is we can just return the letter because we know it's missing.
This solution works, but it isn't ideal because it uses more space (because of our seen map), unlike the other solutions.
const findMissingLetter = (array) => {
const convertUpperCase = array[0] === array[0].toUpperCase() ? true : false;
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const seen = {};
let foundFirstLetter = false;
// add the words we have run across to our seen map
for(const letter of array){
seen[letter.toLowerCase()] = true;
}
// loop through our alphabet checking if letter is in our map. If we find the first letter, then that means
// if we don't find the next letter in our map then it's missing.
for(const letter of alphabet){
if(seen.hasOwnProperty(letter)){
foundFirstLetter = true;
}
if(foundFirstLetter){
if(!seen.hasOwnProperty(letter)){
if(convertUpperCase){
return letter.toUpperCase();
} else {
return letter;
}
}
}
}
}
function fearNotLetter(str) {
var string = array. join("");
for (var i = 0; i < string. length; i++) {
if (string. charCodeAt(i + 1) - string. charCodeAt(i) != 1) {
return String. fromCharCode(string. charCodeAt(i) + 1);
}
}
return undefined
}

return the first non repeating character in a string in javascript

So I tried looking for this in the search but the closest I could come is a similar answer in several different languages, I would like to use Javascript to do it.
The problem is I have an arbitrary string that I would like to return the first non repeating character. EX: 'aba' -> would return b
'aabcbd' -> would return c.
This is what I have so far, just a simple for loop to start.
var someString = 'aabcbd';
var firstNonRepeatedCharacter = function(string) {
for(var i = 0; i < someString.length; i++){
}
};
http://jsfiddle.net/w7F87/
Not sure where to go from here
You can use the indexOf method to find the non repeating character. If you look for the character in the string, it will be the first one found, and you won't find another after it:
function firstNonRepeatedCharacter(string) {
for (var i = 0; i < string.length; i++) {
var c = string.charAt(i);
if (string.indexOf(c) == i && string.indexOf(c, i + 1) == -1) {
return c;
}
}
return null;
}
Demo: http://jsfiddle.net/Guffa/Se4dD/
If you're looking for the first occurrence of a letter that only occurs once, I would use another data structure to keep track of how many times each letter has been seen. This would let you do it with an O(n) rather than an O(n2) solution, except that n in this case is the larger of the difference between the smallest and largest character code or the length of the string and so not directly comparable.
Note: an earlier version of this used for-in - which in practice turns out to be incredibly slow. I've updated it to use the character codes as indexes to keep the look up as fast as possible. What we really need is a hash table but given the small values of N and the small, relative speed up, it's probably not worth it for this problem. In fact, you should prefer #Guffa's solution. I'm including mine only because I ended up learning a lot from it.
function firstNonRepeatedCharacter(string) {
var counts = {};
var i, minCode = 999999, maxCode = -1;
for (i = 0; i < string.length; ++i) {
var letter = string.charAt(i);
var letterCode = string.charCodeAt(i);
if (letterCode < minCode) {
minCode = letterCode;
}
if (letterCode > maxCode) {
maxCode = letterCode;
}
var count = counts[letterCode];
if (count) {
count.count = count.count + 1;
}
else {
counts[letterCode] = { letter: letter, count: 1, index: i };
}
}
var smallestIndex = string.length;
for (i = minCode; i <= maxCode; ++i) {
var count = counts[i];
if (count && count.count === 1 && count.index < smallestIndex) {
smallestIndex = count.index;
}
}
return smallestIndex < string.length ? string.charAt(smallestIndex) : '';
}
See fiddle at http://jsfiddle.net/b2dE4/
Also a (slightly different than the comments) performance test at http://jsperf.com/24793051/2
var firstNonRepeatedCharacter = function(string) {
var chars = string.split('');
for (var i = 0; i < string.length; i++) {
if (chars.filter(function(j) {
return j == string.charAt(i);
}).length == 1) return string.charAt(i);
}
};
So we create an array of all the characters, by splitting on anything.
Then, we loop through each character, and we filter the array we created, so we'll get an array of only those characters. If the length is ever 1, we know we have a non-repeated character.
Fiddle: http://jsfiddle.net/2FpZF/
Two further possibilities, using ECMA5 array methods. Will return undefined if none exist.
Javascript
function firstNonRepeatedCharacter(string) {
return string.split('').filter(function (character, index, obj) {
return obj.indexOf(character) === obj.lastIndexOf(character);
}).shift();
}
console.log(firstNonRepeatedCharacter('aabcbd'));
On jsFiddle
Or if you want a bit better performance, especially on longer strings.
Javascript
function firstNonRepeatedCharacter(string) {
var first;
string.split('').some(function (character, index, obj) {
if(obj.indexOf(character) === obj.lastIndexOf(character)) {
first = character;
return true;
}
return false;
});
return first;
}
console.log(firstNonRepeatedCharacter('aabcbd'));
On jsFiddle
I came accross this while facing similar problem. Let me add my 2 lines.
What I did is a similar to the Guffa's answer. But using both indexOf method and lastIndexOf.
My mehod:
function nonRepeated(str) {
for(let i = 0; i < str.length; i++) {
let j = str.charAt(i)
if (str.indexOf(j) == str.lastIndexOf(j)) {
return j;
}
}
return null;
}
nonRepeated("aabcbd"); //c
Simply, indexOf() gets first occurrence of a character & lastIndexOf() gets the last occurrence. So when the first occurrence is also == the last occurence, it means there's just one the character.
Here's a Solution using Regex to replace all repeating characters and then returning the first character.
function firstNonRepeat(str) {
// Sorting str so that all repeating characters will come together & replacing it with empty string and taking first character using substr.
var rsl = str.split('').sort().join('').replace(/(\w)\1+/g,'').substr(0,1);
if(rsl) return rsl;
else return 'All characters are repeated in ' + str;
}
console.log(firstNonRepeat('aaabcccdeeef'));
console.log(firstNonRepeat('aaacbdcee'));
console.log(firstNonRepeat('aabcbd'));
First of all, start your loop at 1, not 0. There is no point in checking the first character to see if its repeating, obviously it can't be.
Now, within your loop, you have someString[i] and someString[i - 1]. They are the current and previous characters.
if someString[i] === someString[i - 1] then the characters are repeating, if someString[i] !== someString[i - 1] then they are not repeating, so you return someString[i]
I won't write the whole thing out for you, but hopefully the thought process behind this will help
function FirstNotRepeatedChar(str) {
var arr = str.split('');
var result = '';
var ctr = 0;
for (var x = 0; x < arr.length; x++) {
ctr = 0;
for (var y = 0; y < arr.length; y++) {
if (arr[x] === arr[y]) {
ctr+= 1;
}
}
if (ctr < 2) {
result = arr[x];
break;
}
}
return result;
}
console.log(FirstNotRepeatedChar('asif shaik'));
Here's an O(n) solution with 2 ES6 Sets, one tracking all characters that have appeared and one tracking only chars that have appeared once. This solution takes advantage of the insertion order preserved by Set.
const firstNonRepeating = str => {
const set = new Set();
const finalSet = new Set();
str.split('').forEach(char => {
if (set.has(char)) finalSet.delete(char);
else {
set.add(char);
finalSet.add(char);
}
})
const iter = finalSet.values();
return iter.next().value;
}
let arr = [10, 5, 3, 4, 3, 5, 6];
outer:for(let i=0;i<arr.length;i++){
for(let j=0;j<arr.length;j++){
if(arr[i]===arr[j+1]){
console.log(arr[i]);
break outer;
}
}
}
//or else you may try this way...
function firstDuplicate(arr) {
let findFirst = new Set()
for (element of arr)
if (findFirst.has(element ))
return element
else
findFirst.add(element )
}
function firstUniqChar(str) {
let myMap = new Map();
for(let i = 0; i < str.length; i++) {
let char = str.charAt(i);
if(!myMap.has(char)) {
myMap.set(char, 0);
}
myMap.set(char, myMap.get(char) + 1 );
}
for(let [key, value] of myMap) {
if(value === 1) {
return key;
}
}
return null;
}
let result = firstUniqChar("caabbdccee");
console.log(result);
You can use Map Object and set key and value, where in value you store the count for that particular character, After that you can iterate over map and check where is value 1 and return that key.
Map Object remembers the original insertion order of the keys.
This solution should works with array with integers and string.
function firstNoneRepeating(list, map = new Map()) {
for (let item of list) {
if (map.has(item)) {
map.set(item, map.get(item) + 1);
} else {
map.set(item, 1);
}
}
for (let [key, value] of map.entries()) {
if (value === 1) {
return key;
}
}
}
console.log(firstNoneRepeating("aabcbd"));
console.log(firstNoneRepeating([5, 2, 3, 4, 2, 6, 7, 1, 2, 3]));
let str='aabcbd'
let ans=''
for (let i=0;i<str.length;i++){
if(str.indexOf(str.charAt(i))===str.lastIndexOf(str.charAt(i))){
ans+=str.charAt(i)
break
}
}
console.log(ans)
Fill an empty array with zeros, with same length as the string array, and tally up how many times they appear through the loop. Grab the first one in the tallied array with a value of 1.
function firstNotRepeatingCharacter(s) {
const array = s.split("");
let scores = new Array(array.length).fill(0);
for (let char of array) {
scores[array.indexOf(char)]++;
}
const singleChar = array[scores.indexOf(1)];
return singleChar ? singleChar : "_"
}
You can iterate through each character to find() the first letter that returns a single match(). This will result in the first non-repeated character in the given string:
const first_nonrepeated_character = string => [...string].find(e => string.match(new RegExp(e, 'g')).length === 1);
const string = 'aabcbd';
console.log(first_nonrepeated_character(string)); // c
Here is my solution which have time complexity of o(n)
function getfirstNonRepeatingCharacterInAString(params) {
let count = {};
for (let i = 0; i < params.length; i++) {
let count1 = 0;
if (!count[params.charAt(i)]) {
count[params.charAt(i)] = count1 + 1;
}
else {
count[params.charAt(i)] = count[params.charAt(i)] + 1;
}
}
for (let key in count) {
if (count[key] === 1) {
return key;
}
}
return null;
}
console.log(getfirstNonRepeatingCharacterInAString("GeeksfoGeeks"));
Here is my solution using forEach and convert the string into an array
function firstNotRepeatingCharacter(s) {
var strArr = s.split("");
var found = "_";
strArr.forEach(function(item, index) {
if (strArr.indexOf(item) == index && strArr.indexOf(item, index + 1) == -1) {
if (found === "_") {
found = item;
}
}
})
return found;
}
firstNotRepeatingCharacter("abacabad")
Here is another approach:
Everytime you find equal chars store it in an array and break out of the loop. If the char is not found in the array then you have your first nonRepeating char
function nonRepeatingChars(value) {
const memory = []
for (let i = 0; i < value.length; i++) {
for (let j = i + 1; j < value.length; j++) {
if (value[i] === value[j]) {
memory.push(value[j])
break;
}
}
if (!memory.some(x => x === value[i])) {
return value[i];
}
}
return "all chars have duplicates";
}
console.log('First non repeating char is:',nonRepeatingChars("esen"))
console.log('First non repeating char is:',nonRepeatingChars("esesn"))
console.log('First non repeating char is:',nonRepeatingChars("eseulsn"))
console.log('First non repeating char is:',nonRepeatingChars("esesnn"))
> var firstNonRepeatedCharacter = function (str){
> for(i=0;i<str.length;i++){
> if(str.indexOf(str.charAt(i)) === str.lastIndexOf(str.charAt(i))){
> console.log(str.charAt(i));
> break;
> } } }
>
> firstNonRepeatedCharacter ("areerak");
you can check below link
https://codepen.io/t3veni/pen/wvvxJzm
Easy way to solve this algorithm, very straight forward.
function firstNonRepeatChar(str){
let map = {};
for(let i=0; i<str.length; i++){
if(Object.keys(map).includes(str[i])){
map[str[i]]++
}
else{
map[str[i]] = 1;
}
}
for(let j=0; j< Object.values(map).length; j++){
if(Object.values(map)[j] == 1){
console.log(Object.keys(map)[j]);
return
}
if (j == Object.values(map).length-1 && Object.values(map)[j] != 1){
console.log('_');
return;
}
else{
continue;
}
}
}
nonRepeat("aaabbcccdeeef");
Here is one other solution just using array, using 26 unique character as length of array:
var firstUniqChar = (function(s) {
var arr = [];
var str = s.toLowerCase();
for(let c of str){
let index = c.charCodeAt(0) - "a".charCodeAt(0);
arr[index]? ++arr[index]: arr[index]=1;
}
for(let c of str){
let index = c.charCodeAt(0) - 97;
if(arr[index] == 1){
return c;
};
}
return -1;
}("aabcbd"));
console.log(firstUniqChar);
We can keep track of frequency of each character of the string in an object.
For example : for "aabcbd" we can store the frequency as
{ "a":2, "b":2, "c":1, "d":1 }
This will take O(n) time.
Then we can traverse over this object and find the first character with frequency 1, which will also take O(n) time. So, the time complexity for this approach will be O(n).
const firstNonRepeating=(str)=>{
const obj={};
str.split("").forEach(item=>{
obj[item]
? obj[item]++
: obj[item]=1;
});
const item = Object.keys(obj).find(key=> obj[key] === 1);
return item;
}
Note: I use ES6 Object.keys method which may not work in older
browsers.
//To find first non repeating letter
//It will check for both upper and lower case
//only use one String.indexOf()
var mystr="ohvhvtccggt";
var checkFirstNonRepeating=function(){
var ele=[];
for(var i=0;i<mystr.length;i++) {
var key=mystr.charAt(i);
if(!ele[key])
ele[key]=0;
ele[key]++;
//Just check for second occurance of character
//no need to use indexOf twice
if(mystr.indexOf(key,i+1)==-1 && ele[key]<2)
return mystr[i];
}
return "All repeating letters";
}
console.log(checkFirstNonRepeating());
/*
Input : "ohvhvtoccggt"
Output : All repeating letters
Input :"oohjtht"
Output :j
*/
I used object to keep track of characters count in a string then return the char that has the fa value of 1. Here is a demo:
function firstNotRepeatingCharacter(s) {
// initialize an empty object to store chars
let seen = {};
let letter = '';
// iterate over each char in a string
// if it is already there increase value by one
// else set the value to 1
for(let char of s){
if (seen[char]){
seen[char] +=1;
} else {
seen[char] = 1;
}
}
// iterate over the new constructed object
// if the value is 1 and the output variable is empty
// return the associated key to the value 1
// else return '_'
for(let v in seen){
while(seen[v] == 1 && letter === ''){
letter += v;
return letter;
}
}
return('_');
}
console.log(firstNotRepeatingCharacter("abacabad"));
console.log(firstNotRepeatingCharacter("cbc"));
console.log(firstNotRepeatingCharacter("bcccccccccccccyb"));
console.log(firstNotRepeatingCharacter("aaa"));
The most satisfactory and easy to understand answer is the following.
function firstNotRepeatingCharacter(s) {
const arr = s.split("");
for(let i = 0; i < arr.length; i++){
let chr = arr[i];
if( arr.indexOf(arr[i]) == arr.lastIndexOf(arr[i])){
return arr[i]
}
}
return "_"
}
Explanation: It loops through all the characters of a string from forward and backward and then compares the values. If the index of both forward and backward search is true then it returns that character.
let str = 'aabbcdd';
let val = str.split('').reduce((a, e)=>{ if(str.indexOf(e) == str.lastIndexOf(e)) {a = e }; return a})
console.log(val); // c
the implementation below has a good time complexity and it accounts for letters with different cases:
steps
must touch every character in the string to know if it's repeated or not
function firstNonRepeatingLetter(wordd) {
const chars = {}
let word = wordd.toLowerCase()
// go through chars
// store chars in hash with values of array storing index of char and true if only 1 encountered so far
for (let i = 0; i < word.length; i += 1) {
let char = word[i]
if (chars[char]) {
chars[char][0] = false
} else {
chars[char] = [true, i]
}
}
let output = ''
let index;
for (let key in chars) {
// return char with true and lowest index
if (chars[key][0]) {
index = index === undefined ? chars[key][1] : index
if (index >= chars[key][1]) {
output = key
}
}
}
return index === undefined ? '' : wordd[index]
}
console.log(firstNonRepeatingLetter('sTreSS')) //T```
The bellow solution is a kind of frequency counter pattern and it will run only one loop, so O(n) will be the time complexity.
function firstNotRepeatingCharacter(str) {
const obj = {};
for (let i = 0, L = str.length; i < L; i++) {
const char = str[i];
obj[char] = obj[char] ? obj[char] + 1 : 1;
}
for (let key of Object.keys(obj)) {
if (obj[key] == 1) {
return key;
}
}
return -1;
}
Here is another solution
function firstNotRepeatingCharacter(s) {
const obj = {};
for (let i of s) {
if(!obj[i]) {
obj[i] = 1;
} else if (obj[i]) {
obj[i] = +obj[i] + 1;
}
}
for (let [key, value] of Object.entries(obj)) {
if(value == 1) return key;
}
return "_"
}
Using below method can achieve first non repeated character
function main(str) {
str = String(str).toLowerCase();
let non_repeated_char = 'N/A';
for (let i = 0; i < str.length; i++) {
let currentChar = str[i];
let repeated_times = String(str).split('').filter(e => e == currentChar).length;
if (repeated_times === 1) {
non_repeated_char = currentChar;
break;
}
}
return non_repeated_char;
};
let Result = main("basketball");
console.log("The Non Repeated char is-->", Result);

Finding the most frequent character in a string javascript

Assuming I have the following string "355385". I need a simple JavaScript that can tell me that the most mentioned character is 5. Thank you in advance.
I tried with this one but no results.
var exp = '355385' ;
var exps =exp.split("");
var expCounts = { };
for (var i=0;i<exp.length;i++)
{expCounts["_" + exps[i]] = (expCounts["_" + exps[i]] || 0) + 1 ;
if (expCounts==3) exps=exps[i]; }; exps;
This will loop over every character in the string and keep track of each character's count and the character with the maximum count:
var exp = '3553853335' ;
var expCounts = {};
var maxKey = '';
for(var i = 0; i < exp.length; i++)
{
var key = exp[i];
if(!expCounts[key]){
expCounts[key] = 0;
}
expCounts[key]++;
if(maxKey == '' || expCounts[key] > expCounts[maxKey]){
maxKey = key;
}
}
console.debug(maxKey + ":" + expCounts[maxKey]);
Update:
Here is an ES6 version that will handle strings where multiple character have the same max count
function maxCount(input) {
const {max, ...counts} = (input || "").split("").reduce(
(a, c) => {
a[c] = a[c] ? a[c] + 1 : 1;
a.max = a.max < a[c] ? a[c] : a.max;
return a;
},
{ max: 0 }
);
return Object.entries(counts).filter(([k, v]) => v === max);
}
Example (please excuse the crude output):
maxCount('--aaaa1111--').join(' | ').replace(/,/g, ':');
outputs 1:4 | -:4 | a:4
var getMax = function (str) {
var max = 0,
maxChar = '';
str.split('').forEach(function(char){
if(str.split(char).length > max) {
max = str.split(char).length;
maxChar = char;
}
});
return maxChar;
};
logs
getMax('355385') //5;
getMax('35538533') //3;
in equal case it will return first number
getMax('3553') //3;
var string = "355385",
counter = {};
for (var i = 0, len = string.length; i < len; i += 1) {
counter[string[i]] = (counter[string[i]] || 0) + 1;
}
var biggest = -1, number;
for (var key in counter) {
if (counter[key] > biggest) {
biggest = counter[key];
number = key;
}
}
console.log(number);
# 5
var exp = '355385';
var findMostFrequent = function (string) {
var chars = {}, first = string.charAt(0);
chars[first] = 1;
var maxChar = first, maxCount = 1;
for (var i = 1; i < string.length; i++) {
var char = string.charAt(i);
if (chars[char]) {
chars[char]++;
} else {
chars[char] = 1;
}
if (chars[char] > maxCount) {
maxChar = char;
}
}
return maxChar;
};
Another Solution
function maxChar(str) {
const charMap = {};
let max = 0;
let maxChar = '';
for(let char of str){
if(charMap[char]){
charMap[char]++;
}else{
charMap[char] = 1;
}
}
for(let char in charMap){
if(charMap[char] > max){
max = charMap[char];
maxChar = char;
}
}
return maxChar;
}
Result:
maxChar('355385')
"5"
Another way to get the most frequent character in a string - sort frequency map into an array and then return the first (greatest) value from that array:
function highest (string) {
let array = Array.from(string);
let frequencyMap = {};
array.forEach((value, index) => {
if (!frequencyMap[value]) {
frequencyMap[value] = 0;
}
frequencyMap[value] += 1;
})
let frequencyArray = Object.entries(frequencyMap);
frequencyArray.sort((a, b) => {
if (a[1] < b[1]) {
return 1;
}
if (a[1] > b[1]) {
return -1;
}
return 0;
});
return(frequencyArray[0][0]);
}
console.log(highest("hello World"));
returns "l"
None of the answers above take into consideration that JavaScript internally uses UTF-16
const s = "πŸ˜„πŸ˜…πŸ˜„πŸ˜„πŸ˜…πŸ˜…πŸ˜„πŸ˜„πŸ˜±πŸ˜±πŸ˜„";
function getMostFrequentChar(s) {
const len = s.length;
const freq = {};
let maxFreq = 0;
let maxChar;
for (let i = 0; i < len; ++i) {
const isPair = (s.charCodeAt(i) & 0xF800) == 0xD800;
const c = isPair ? s.substr(i++, 2) : s[i];
const f = (freq[c] || 0) + 1;
freq[c] = f;
if (f > maxFreq) {
maxFreq = f;
maxChar = c;
}
}
return {maxFreq, maxChar, freq}
}
console.log(getMostFrequentChar(s));
Note: the code above assumes the string is valid UTF-16. It's possible to construct a string that is not valid UTF-16 in which case maybe you could change isPair to
const isPair = len - i > 1 &&
s.charCodeAt(i ) & 0xF800) == 0xD800 &&
s.charCodeAt(i + 1) & 0xF800) == 0xD800;
But it's not clear what a character with an invalid UTF-16 value means.
It also won't handle more funky unicode
s = "πŸ‘¦πŸΏπŸ‘¦πŸ‘¦πŸΏπŸ‘¦πŸ‘¦πŸ»πŸ‘¦πŸ½πŸ‘¦πŸΎπŸ‘¦πŸΏ"
There are many graphmemes that take multiple unicode code points
Also, splitting the string using split is SSSSSSLLLLLOOOOWWWW and a huge memory hog if the string is long.
Here is yet another answer to this question:
For this I have considered that the character can be of whatevert kind except a space
function findHighestFreqInString(str) {
if (!str) return null
let cleanedStr = str.replace(/\s/g, '') //assumes no spaces needed
if (cleanedStr.length === 0) return null
let strObj = {}
let topChar = ''
for (let val of cleanedStr) {
strObj[val] = (strObj[val] || 0) + 1
if (topChar === '' || strObj[val] >= strObj[topChar]) topChar = val
}
return topChar
}
Here is how you would use it:
findHighestFreqInString('my name is Someone') // returns: e
findHighestFreqInString('') // returns: Null
findHighestFreqInString(' ') // returns: Null
Here is:
let str = '355385';
function mostFrequentCharacter(str) {
let charactersArr = str.split(''),
bins = {};
charactersArr.map(el => bins[el] = (bins[el] || 0) + 1);
return Object.keys(bins).map(key => ({
name: key,
count: bins[key]
})).sort((a, b) => b.count - a.count)[0];
}
You can use the following solution to find the most frequent character in a string:
function getMostRepeatedCharacter(string) {
return string.split('').reduce((acc,char)=>{
let len = string.split(char).length - 1;
return len > acc[1] ? [char,len] : acc
},['',0])[0]
}
getMostRepeatedCharacter('wediuaududddd') // d
Want to share this ES6 functional approach. Please provide your input.
function maxChar(myStr) {
let charObj = {};
return [...myStr].reduce((_, char) => {
if (char in charObj) charObj[char]++;
else if (char !== " ") charObj[char] = 1;
return Object.keys(charObj).reduce((a, b) => {
return charObj[a] > charObj[b] ? a : b;
});
});
}
The simplest approach will be like this:
function maxChar(str) {
const charMap = {};
let max = 0;
let maxChar = '';
start by making an object of words and how many they repeated, to do that we have to loop through the string using for of and implementing the conditions:
for (let char of str) {
if (charMap[char]) {
charMap[char]++;
} else {
charMap[char] = 1;
}
}
and now loop through the object using for in
for (let char in charMap) {
if (charMap[char] > max) {
max = charMap[char];
maxChar = char;
}
}
return maxChar;
}
this is another (bizarre) way
It substitute the current character with blank for check how many times is present in the string making the difference of length with original pattern
var str = "355385";
var mostLength = 0;
var characterMostLength;
for(t = 0; t< 10; t++)
{
var res = str.length - str.replace(new RegExp(t, "g"), "").length;
if (res > mostLength){
characterMostLength = t;
mostLength = res;
}
}
function solution(N) {
var textToArr = N.split('');
var newObj = {};
var newArr = [];
textToArr.map((letter) => {
if(letter in newObj){
newObj[letter] = newObj[letter]+1;
} else {
if(letter !== ' '){
newObj = Object.assign(newObj, {[letter]: 1})
}
}
});
for(let i in newObj){
newArr.push({name: i, value: newObj[i]})
}
var res = newArr.sort((a,b) => b.value-a.value)[0];
return res.name+':'+res.value
}
solution("hello world");
this is a simple Idea that only includes one pass-through with a hashmap. The only thing this does not do is handle several max numbers. I really hope you enjoy my solution :) .
function maxChar(str) {
//Create the output and the hashmap
let m = {}, ans
//Loop through the str for each character
//Use reduce array helper because of the accumulator
str.split('').reduce((a, c) => {
//Increments Map at location c(character) unless it does not already exist
m[c] = m[c] + 1|| 1
//This checks to see if the current passthrough of m[c] is greater than or equal to the accumulator, if it is, set the answer equal to the current character. If it's not keep the ans the same.
ans = m[c] >= a ? c : ans
//Only increment the accumulator if Map at location c(character) is greater than the accumulator. Make sure to return it otherwise it won't increment.
return a = m[c] > a ? a + 1 : a
}, 1)
//Lastly return the answer
return ans
}
Simplest way to find maximum number of occurring character in string
var arr = "5255522322";
var freq:any = {};
var num;
for(let i=0;i<arr.length;i++) {
num = arr[i];
freq[num] = freq[num] >= 1 ? freq[num] + 1 : 1;
}
var sortable:any = [];
for(let i in freq)
{
sortable.push(i);
}
var max = freq[sortable[0]];
var data:any = "";
var value = sortable[0];
for(let i=0;i<sortable.length;i++) {
if(max > freq[sortable[i]]){
data = "key" + value + " " + "value" + max;
}else{
value = sortable[i]
max = freq[sortable[i]];
}
}
console.log(data);
function maxChara(string) {
charMap = {};
maxNum = 0;
maxChar = "";
string.toString().split("").forEach(item => {
if (charMap[item]) {
charMap[item]++;
} else {
charMap[item] = 1;
}
});
for (let char in charMap) {
if (charMap[char] > maxNum) {
maxNum = charMap[char];
maxChar = char;
}
}
return maxChar;
}
let result = maxChara(355385);
console.log(result);
Here str will the string that needs to be verified.
function maxCharacter(str){
let str1 = str; let reptCharsCount=0; let ele='';let maxCount=0;
let charArr = str1.split('');
for(let i=0; i< str1.length; i++){
reptCharsCount=0;
for(let j=0; j< str1.length; j++){
if(str1[i] === str1[j]) {
reptCharsCount++;
}
}
if(reptCharsCount > maxCount) {
ele = str1[i];
maxCount = reptCharsCount;
}
}
return ele;
}
input
maxCharacter('asdefdfdsdfseddssdfsdnknmwlqweeeeeeeesssssssssssseeee');
output
"s"
function freq(str) {
var freqObj = {};
str.forEach((item) => {
if (freqObj[item]) {
freqObj[item]++;
}
else {
freqObj[item] = 1;
}
});
return freqObj;
}
function findmaxstr(str) {
let max = 0,res,freqObj;
freqObj = freq(str.split(""));
for(let keys in freqObj){
if (freqObj[keys] > max) {
max = freqObj[keys];
res = keys;
}
}
console.log(res);
return res;
}
findmaxstr("javasdasdsssssscript");
const maxChar = (str) => {
let obj = {};
for (let char of str) {
(!obj[char]) ? obj[char] = 1: obj[char]++;
}
maxCharcount = Math.max(...Object.values(obj));
const key = Object.keys(obj).filter(key => obj[key] === maxCharcount);
console.log(`Most repeated character/characters in the given string "${str}" is/are given below which repeated ${maxCharcount} times`);
console.log(...key);
}
maxChar("355385");
Here is the code, where it also checks for lower and upperCase characters with the same max count and returns a Lower ASCII character as a Max.
function mostFrequent(text) {
let charObj={}
for(let char of text){
if(char!==' '){
if(charObj.hasOwnProperty(char)) charObj[char]=charObj[char]+1;
else charObj[char]= 1
}
}
let maxOccurance= Object.keys(charObj)[0], i=0;
for(let property in charObj){
if(i>0){
if(charObj[property]> charObj[maxOccurance])
maxOccurance= property
else if(charObj[property]=== charObj[maxOccurance])
{
if(property<maxOccurance)
maxOccurance=property
}
}
i++
}
return [maxOccurance, charObj[maxOccurance]]
}
let str = '355385';
let max = 0;
let char = '';
str.split('').forEach((item) => {
let current = str.split(item).length;
if (current > max) {
max = current;
char = item;
}
});
console.log(char + ' occurred ' + (max - 1) + ' times');
var exp = '35585' ;
var expCounts = { };
let maxChar = ''
let count = 0
for(let i = 0; i < exp.length; i++){
let char = exp[i]
expCounts[char] = expCounts[char] + 1 || 1
if(expCounts[char] > count){
maxChar = char
count = expCounts[char]
}
console.log(maxChar)
}
function checkNoofOccurenance(string) {
const arr = [...new Set(string.split(''))].sort();
const finalObj = {};
arr.forEach((item) => {
finalObj[item] = string.split(item).length - 1;
});
const item=Object.keys(finalObj).reduce((occ, toBeComapir)=>finalObj[occ]>finalObj[toBeComapir]?occ:toBeComapir)
return item;
}
Using Hasmaps we can find the most frequent char and occurrence all in O(N) time complexity. Below is the code. I have used one hasmap to save all the values and while i am doing it, i am also calculating the max occurrence and the max char.
var mostFreq = function(s) {
let myMap = new Map();
let temp;
let counter = 0;
let mostFrequentChar;
for(let i =0;i <s.length;i++){
if(myMap.has(s.charAt(i))){
temp = myMap.get(s.charAt(i));
temp = temp + 1;
myMap.delete(s.charAt(i));
myMap.set(s.charAt(i) , temp)
if(temp > counter){
counter = temp;
mostFrequentChar = s.charAt(i);
}
}else{
myMap.set(s.charAt(i), 1)
}
}
//if you want number of occerance of most frequent char = counter
//if you want list of each individual char and its occurrence = myMap
//if you just want the char that is most frequence = mostFrequentChar;
return mostFrequentChar;
};
If you want the count of the letter as well, You can do this
const { letter, count } = input.split("").reduce(
(acc, letter) => {
const count = input.split(letter).length - 1;
return count > acc.count
? { letter, count }
: { letter: acc.letter, count: acc.count };
},
{ letter: "", count: 0 }
);
Here We are splitting the string, applying a reduce to the result. The Reduce Counts how many instances of a character are there in a string, using input.split(letter).length - 1; And if the count is greater than the previous count, updates the accumulated value to be the current value
let string = "355385";
function printFirstRepeat(str){
let output= {};
for (let char of str) {
char = char.toLowerCase();
output[char] = ++output[char] || 1;
if(output[char] > 1) return char;
}
return "Not Found"
}
console.log(printFirstRepeat(string));
Algorithm: Find maximum occurring character in a string (time complex: O(N))
I'll provide my solution to this algo-problem by utilizing the most recent concepts of javascript
const getMaxCharacter = (str) => {
let max = 0;
let maxChar = '';
str.split('').forEach((char) => {
if (str.split(char).length > max) {
max = str.split(char).length - 1;
maxChar = char;
}
});
return `The max letter is : ${maxChar} and the max number of times it is seen is: ${max} times`;
};
Let's express an easy way of testing the function logic I wrote it:
const letter = 'Hello Student';
getMaxCharacter(letter);
In the function developed, I've used the concepts below:
Arrow Function
Anonymous Funciton
Declare property by using let/const
Template Literals
forEach(); (array helper) & split()
This is simple and optimized solution and it returns the first occurring char if there are chars equals in counts
function maxOccurance(str) {
let maxOccurringChar = "";
const charMap = {};
for (let index = 0; index < str.length; index++) {
const ele = str.charAt(index);
if (!charMap[ele]) {
charMap[ele] = {
startIndex: index,
value: 1
};
} else {
charMap[ele].value = charMap[ele].value + 1;
}
if (
!maxOccurringChar ||
charMap[maxOccurringChar].value < charMap[ele].value
) {
maxOccurringChar = ele;
} else if (
charMap[maxOccurringChar].value === charMap[ele].value &&
charMap[ele].startIndex < charMap[maxOccurringChar].startIndex
) {
maxOccurringChar = ele;
}
}
return maxOccurringChar;
}
console.log( maxOccurance("bacdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
);
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<p id = "myString">Hello World! I am Julio!</p>
<p id = "mRCharacter"></p>
<script>
var string = document.getElementById("myString").innerHTML;
var mRCharater = mostRepetedCharacter(string);
document.getElementById("mRCharacter").innerHTML = mRCharater;
console.log(mRCharater);
function mostRepetedCharacter(string){
var mRCharater = "";
var strLength = string.length;
var i = 0;
var count = 0;
var max = 0;
var rest = strLength - 1;
while (i < strLength){
var j = i + 1;
while (j <= rest){
if (string[i] === string[j]){
count++;
}
if (count > max){
max = count;
mRCharater = string[i];
}
j++;
}
i++;
count = 0;
}
return mRCharater;
}
</script>
</body>
</html>
enter code here

Javascript: Determine if all characters in a string are unique and if not, delete duplicate characters

Have an array set up with a[letter][occurences], but struggling with looping through this array, to check for occurences > 1 and removing the ones that are.
function charFreq(s) {
var i, j;
var a = new Array();
for (j = 0; j < s.length; j++) {
for (i = 0; i < a.length; i++) {
if (a[i][0] == s[j]) {
a[i][1]++;
break;
}
}
if (i == a.length) {
a[i] = [s[j], 1];
}
}
return a[i][0];
}
document.write(charFreq("insert string here"));
This is the mess I've come up with so far:
function check(str) {
var c;
for (c=0; c < a.length; c++) {
if(a[c][1] == 1) {
return true;
break;
} else {
return false;
}
}
}
Using ES6 Set:
// :: unique = Array<any>|string => Array<any>
const unique = xs => [...new Set(xs)]
const dedupe = str => unique(str).join('')
console.log(
unique('foo'), // => ['f', 'o']
dedupe('foo'), // => 'fo'
)
Don't do it that way.
function noDups( s ) {
var chars = {}, rv = '';
for (var i = 0; i < s.length; ++i) {
if (!(s[i] in chars)) {
chars[s[i]] = 1;
rv += s[i];
}
}
return rv;
}
alert(noDups("Shoe fly pie, and apple pan dowdy")); // Shoe flypi,andw
As the length of your string gets longer, your code gets slower by a factor roughly equal to the square of the length of the string.
To delete duplicate characters from an string, you can use the next function that made the user #Cerbrus
function find_unique_characters( string ){
var unique='';
for(var i=0; i<string.length; i++){
if(string.lastIndexOf(string[i]) == string.indexOf(string[i])){
unique += string[i];
}
}
return unique;
}
console.log(find_unique_characters('baraban'));
If you only want to return characters that appear occur once in a
string, check if their last occurrence is at the same position as
their first occurrence.
Your code was returning all characters in the string at least once,
instead of only returning characters that occur no more than once
Link to the thread of stackoverflow Remove duplicate characters from string
​
Here's a quick way:
str = str.split('').filter(function(v,i,self){
return self.indexOf(v) == i;
}).join('');
function RemoveDuplicateLetters(input) {
var result = '', i = 0, char = '';
while (i < input.length) {
char = input.substring(i, i+1);
result += char;
input = input.replace(char,'');
}
return result;
}
I can't see a splice version, so here's one:
function uniqueChars(s) {
var s = s.split('');
var c, chars = {}, i = 0;
while ((c = s[i])) {
c in chars? s.splice(i, 1) : chars[c] = ++i;
}
return s.join('');
}
This assumes only alpha characters, and upper case not equal to lower case.
function uniqueChars(string){
var i= 0, L= string.length, ustring= '', next;
while(i<L){
next= string.charAt(i++);
if(ustring.indexOf(next)== -1) ustring+= next;
}
return ustring.replace(/[^a-zA-Z]/g, '');
}
var s1= 'The quick red fox jumps over the lazy brown dog.';
uniqueChars(s1)
/* returned value: (String)
Thequickrdfoxjmpsvtlazybwng
*/
This returns any unique character-
function uniqueArray(array){
return array.filter(function(itm, i, T){
return T.indexOf(itm)== i;
});
}
var s1= 'The quick red fox jumps over the lazy brown dog.';
uniqueArray(s1.split('')).join('');
/* returned value: (String)
The quickrdfoxjmpsvtlazybwng.
*/

Categories

Resources