JS - getting the longest non-repeating character substring from a string - javascript
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("2sLbj8wBPPa5RYgNNwzuYQ7Pz3IehWrxOPiAxbGTw1wpEKyPnWLEuiEiW8vPFGENfjICBeNBeslmT8UZZD7AyUp1WKrC8OneS7GzfacqpzzGB4awH4O7CosGQp3fe3UYTqPT44hHtYfGTLKaZkIt6dA1XQQCzvUYCQFFEGWxzPDFZt2D7a8XT4UEuBrUYo4ml4xds7cQflVD8Hyr9bJcCfC6BeUutQb4SrqUR3SlLNVMVlXEYsHLKbXjl0zYNFeIatfvx5w7UNEfLGqhtzNvGKVixQQ6OIIH835jo7IYp6G7FumW9asgKSjbbEQhfWKenep8yL3psmb1eUl2vWGxDKo8Z82UB4M5LcZ4o2W9nGHrpAkyOdJb6WIc3sfeThMAUHA7Ykh9bNmDrmW7J44eJ1XLbrN6xHnUC8LrGXYlFCh7x1bkjyy08Z7LVQGhnuDU1ZaovmKoTrtgPzvIEESakLXxB2t0RbrTVrnGcstUKtTyHqj8X6e3V0lqHjzGZ0Hpucp0cuJLSHOF4GTnju6JK74EbjN5Di9gy7qbe7K59mjdzlzsGHMRq9POl86bFUOgxyFmHWrPRkQXC1TS5YCUiy1Bbu6MXTLvbL3YnKhyLSWh3lplRlPmkEIYVvgDrUa40n9qNFSdMJya4zH3JJ6J2k5ni4hjlg2IIbpG8FbiTqt24jbFbZQOAhG1DAi7VmySqpaaGiKVmpsVc0iaxGiL1mItDaZlRFox7LgJDmgQJbk7reHfq8FH1uLLSy1py5cyt0QlQwkjZrQAYjbKK3fCSLEMCmDQkhcVOoGpg6YaU7wvoroY4rH1IrB03R0WanRjWKPdGmARZaENOMf9MjfZjonrcaJQDqYUsps4G4i4BDcD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qd0FrVNfawh7PW3CCbKCxuEj03PCnW3NzAgS9FPlMl5kZtOMx2T59SRw9qnbCs37ghJtMYgI1iaEq4ILNnVrDZ2OMFO3Pmp1iDLuudI9ubvi7HVclBfz0i8z5CJmbNN1GRsXtsUFxqauyAeO27ECVHHiJBCpoWmVh4FcsGcFD8ISFcPDQCADNomO5htszRRQRrxKYk7KimjiDh0B3NdEP6H7fttigwfTWvjhYBEB8fnET9wqCAyMonXaaPkq73N7TUdlFSsxCdNkixI64ksu4go1gdB4YxZ9ALoloPc2vUpjn18EX3x0EVabXKEHWNIgyfcFdbrjU6E3cQ6udgfH8AtWj8yhxwgK4P0fUqqJg3le0MPo5KybEWuTtoBsdcQ4WFYxuUcTAXF2hwdTwMhoST9OwKL4K4eRuA1g4SQqesLZmMFwXsCFKHRSO8bv48qplLX5QnGN7qm85OygqsV5NsxTpLCG8QSk7ribN32iUfkdPc53PTmSDL8HJGzVh5jaCE14StYPBd8g1uDK3SM5F2RQPV6pzUQDxhgAMJFv5Zp7bQ4SrqCfZxiCp1prx408tK3icnp6GyZCM2nFuLopKHE6X8LJB44Z8XjXBipeM5rQClrhpxb0TBTkmhWdEer1pAkTnMHX4PLCeowMaz5Q2KG7cEhY8VJjOhK22Ln"))
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))
Related
How to count common characters in two strings in JavaScript?
Given two strings s1 and s2 consisting of lowercase English alphabets, the task is to count all the pairs of indices (i, j) from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i] pairs with some s2[j] then these two characters will not be paired with any other character. Input: s1 = 'abcd', s2 = 'aad' Output: 2 Input: s1 = 'geeksforgeeks', s2 = 'platformforgeeks' Output: 8 I tried to like this: function getSameCount(str, str2) { var o = {}, o2 = {}; for (var i = 0; i < str.length - 1; i++) { if (str[i] in o) { o[str[i]] = parseInt(o[str[i]] + 1) } else { o[str[i]] = 0 } } console.log(o); for (var i = 0; i < str2.length - 1; i++) { if (str[i] in o2) { o2[str[i]] = parseInt(o2[str[i]] + 1) } else { o2[str[i]] = 0 } } console.log(o2); } getSameCount('abcd', 'aad')
Use for..in loop and includes method var s1 = "abcd"; var s2 = "aad"; function match(s1, s2) { var count = 0; for(let i in s1) { s2.includes(s1[i]) ? count++ : false; } return count; } console.log(match(s1,s2));
We can convert the second input string to an array, then the next step is to iterate over the first input string and find a match in the second input string's character array. If a match is found, increment the counter and remove that character from the second input string's character array so that it is not considered in the next match: //Solution: function getSameCount(str1, str2) { let count = 0; const obj = str2.split(""); for(str of str1){ let idx = obj.findIndex(s => s === str); if(idx >= 0){ count++; obj.splice(idx, 1); } } return count; } //Test: console.log(getSameCount("abcd", "aad")); console.log(getSameCount("geeksforgeeks", "platformforgeeks")); console.log(getSameCount("aad", "abcd")); console.log(getSameCount("platformforgeeks", "geeksforgeeks"));
You can create a custom method on the array and find the number of characters which are common in all the words. Below steps are list of procedure to find the common characters in all the string Create a prototype method on Array , findCommonWord in this case. This method accepts an array of string, so input will be like [ "abcd", "aad","geeksforgeeksda","platdformforgeeks"].findCommonWord() First step is to modify the input, to remove duplicate characters from a string using Set then sort it by ascending order of the length of string. This is because number of loop will be least if we have to find common character , that also have to present in string with least length. Then create a new array with out the first string and split the first string. split will create a new array and iterate over it and check if this character is present in rest of the string. var s1 = "abcd", s2 = "aad", s3 = "geeksforgeeksda", s4 = "platdformforgeeks"; Array.prototype.findCommonWord = function() { let tempArray = this.map(function(item) { return [...new Set(item.split(''))].join(''); }).sort(function(a, b) { return a.length - b.length }) let count = 0; let firstElm = tempArray[0].split(''); let restElem = tempArray.splice(1); let countObject = {} for (let i = 0; i < firstElm.length; i++) { let z = findIfIncludes(restElem, firstElm[i]); if (z.length === restElem.length) { countObject[firstElm[i]] = 1; } else { countObject[firstElm[i]] = 0 } } function findIfIncludes(arr, char) { return arr.filter(item => item.includes(char)) } console.log(countObject) let totalCount = 0; for (let keys in countObject) { if (countObject[keys] > 0) { totalCount += 1; } } return totalCount; }; console.log([s1, s2, s3, s4].findCommonWord());
function numberOfSameChars(s1, s2) { let obj = {}; let counter = 0; for (let i=0; i<s1.length; i++){ if(s1[i] in obj) { obj[s1[i]]++; } else { obj[s1[i]] = 1; } } for (let i=0; i<s2.length; i++) { if(s2[i] in obj && obj[s2[i]] > 0) { obj[s2[i]]--; counter++; } } return counter; }
Try this code: function countMatch(s1,s2){ var count = 0; while(s1.length && s2.length){ if(s2.includes(s1.charAt(0))){ count++; s2 = s2.replace(s1.charAt(0),""); s1 = s1.slice(1); } else { s1 = s1.slice(1); } } return count; } console.log(countMatch("abcd","aad")); //2
I used objects to do this, and I was kind of curious about there being another way, as I wanted to take an algorithmic approach as well, but whatever works works. Anyways, here's the code: function commonCharacterCount(s1, s2) { let string1Counter = {}; let string2Counter = {}; let commonCount = 0; for(let i = 0; i < s1.length; i++){ if(!string1Counter.hasOwnProperty(s1[i])){ string1Counter[s1[i]] = 0; } string1Counter[s1[i]]++; } for(let i = 0; i < s2.length; i++){ if(!string2Counter.hasOwnProperty(s2[i])){ string2Counter[s2[i]] = 0; } string2Counter[s2[i]]++; } for(let key in string1Counter){ if(string2Counter.hasOwnProperty(key)){ if(string1Counter[key] < string2Counter[key]){ commonCount += string1Counter[key]; } else{ commonCount += string2Counter[key]; } } } return commonCount; } The logic is basically to just save all of the characters of each string and their count,check for similarities, and compare the count of common characters. Whichever has the fewer amount of common characters will be the amount shared by both. O(3N) time complexity, O(2N) space complexity (because of the stored objects). I guess I can also do "delete object" but seems redundant on just an algorithm IDE because it's not like it's running on a server for any extended period of time.
function commonSameCount(s1, s2) { var s1Array = s1.split(""); var s2Array = s2.split(""); var count = 0; let index = 0; s1Array.filter(s1 => { index = s2Array.findIndex(s2 => s2 == s1); if(index >= 0){ count++; s2Array.splice(index, 1); } }); return count; }
A simple solution using Regex: index.js function getSameCount(s1,s2) { for (let i in s2) { s1 = s1.replace(s2[i], "1") } const result = s1.replace(/[^1]/g, '').length return result }
how to get common substring in two string to maintain order?
I am trying to do one problem of hackerrank but I am not able to solve that problem Can someone please help me with wrong logic implementation done by me? problem Print the length of the longest string, such that is a child of both s1 and s2. Sample Input HARRY SALLY Sample Output 2 Explanation The longest string that can be formed by deleting zero or more characters from HARRY and SALLY is AY, whose length is 2. Sample Input 1 AA BB Sample Output 1 0 Explanation 1 AA and BB have no characters in common and hence the output is 0 Sample Input 2 SHINCHAN NOHARAAA Sample Output 2 3 Explanation 2 The longest string that can be formed between SHINCHAN and NOHARAAA while maintaining the order is NHA. I have written some logic which is as follows: function commonChild(s1, s2) { var arr = s2.split(), currenString = '', maxLength = 0, index = -1; console.log(arr); for (var i = 0; i < s1.length; i++) { var char = s1.charAt(i), charIndex = arr.indexOf(char); console.log(char) if (index < charIndex) { index = charIndex; currenString +=char; } maxLength= Math.max(maxLength,currenString.length) } return maxLength; } commonChild('ABCDEF', 'FBDAMN'); console.log(commonChild('ABCDEF', 'FBDAMN'));
pardon me. this is an unoptimized solution. function maxCommon(a, b, offset) { offset = offset || 0; if (a === b) { return [[a, b]]; } var possibleSolns = []; for (var i = 0 + offset; i < a.length; i++) { for (var j = 0 + offset; j < b.length; j++) { if (a.charAt(i) === b.charAt(j)) { possibleSolns.push([ a.substring(0, offset) + a.substring(i), b.substring(0, offset) +b.substring(j) ]); break; } } } var results = []; possibleSolns.forEach(function(soln) { var s = maxCommon(soln[0], soln[1], offset+1); if (s.length === 0) { s = [[soln[0].substring(0, offset +1), soln[1].substring(0, offset +1)]]; } results = results.concat(s); }); return results; } var maxLen = -1; var soln = null; maxCommon("ABCDEF", "FBDAMN").map(function(_) { return _[0]; }).forEach(function(_) { if (_.length > maxLen) { soln = _; maxLen = _.length; } }); console.log(soln);
I kept most of your logic in the answer: function commonChild(s1, s2) { var // Sets strings to arrays arrayString1 = s1.split(""), arrayString2 = s2.split(""), collectedChars = "", maxLength = 0, max = arrayString1.length; for (var i = 0; i < max; i++) { var char = arrayString1[i], count = arrayString2.indexOf(char); // check if char is in second string and not in collected if (count != -1 && collectedChars.indexOf(char) == -1) { collectedChars += char; maxLength++; } } return maxLength; } // expected output 4 console.log(commonChild( 'ABCDEF', 'FBDAMN' )); // expected output 1 console.log(commonChild( 'AA', 'FBDAMN' ));
Using lodash and spread operation you can do it in this way. const test = (first, second) => { const stringArray1 = [...first]; const stringArray2 = [...second]; return _.intersection(stringArray1, stringArray2).length; } console.log(test('ABCDEF', 'FBDAMN'));
You can solve it using lcs least common subsequence function LCS(s1,s2,x,y){ var result = 0; if(x==0 || y==0){ result = 0 }else if(s1[x-1] == s2[y-1]){ result = 1+ LCS(s1,s2,x-1,y-1) } else if(s1[x-1] != s2[y-1]){ result = Math.max(LCS(s1,s2,x-1,y), LCS(s1,s2,x,y-1)) } return result; } // Complete the commonChild function below. function commonChild(s1, s2) { return LCS(s1,s2,s1.length,s2.length); }
Based on your code before the edit. One little change is to change var arr = s2.split() to split(''). The main change in the logic is that I added a loop to run over the string each time from another character (first loop from the first, second from the second etc). function commonChild(s1, s2) { var arr = s2.split(''), currenString = '', maxLength = 0, index = -1, j = -1; for (var ii = 0; ii < s1.length; ii++) { index = -1; currenString = ''; for (var i = ii; i < s1.length; i++) { var char = s1.charAt(i), j = arr.indexOf(char); if (index < j) { index = j; currenString += char; } maxLength = Math.max(maxLength, currenString.length) } } return maxLength; } console.log(commonChild('ABCDEF', 'FBDAMN'));
Check for repeated characters in a string 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
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
Using Javascript find array values in another array and in order
Here is one to get your brain going! I've not had any luck with it. [1,2,1,1,2,1,1,1,2,2] [1,2,1,1,2,1] I would like to use the second array to find the values in the first, but they must be in the same order. Once for I would like it to return the next key up from the last key in the second array. So in this example it would use the first six digits in the first array and then return 6 as the key after the final one in the second array.
var a2 = [1,2,1,1,2,1,1,1,2,2] var a1 = [1,2,1,1,0,1] function find(arr1, arr2) { var len = 1 var result = 0; var s2 = arr2.toString(); for (len=1;len <= a1.length; len++) { var aa1 = arr1.slice(0, len) var s1 = aa1.toString(); if(s2.indexOf(s1)>=0){ result = aa1.length; } else { break; } } return result; } alert(find(a1, a2));
var find = function(haystack, needle) { var doesMatch = function(offset) { for (var i = 0; i < needle.length; i++) { if (haystack[i+offset] !== needle[i]) { return false; } } return true; }; for (var j=0; j < haystack.length - needle.length; j++) { if (doesMatch(j)) { return j; } } return -1; };
This is quick, this is dirty, and this is correct only if your data doesn't include any comma. var needle = [1,2,1,1,2,1]; var haystack = [1,2,1,1,2,1,1,1,2,2]; if ( needle.length <= 0 ) return 0; var fromStr = ','+haystack.toString()+',' var findStr = ','+needle.toString()+',' // Find ',1,2,1,1,2,1,' in ',1,2,1,1,2,1,1,1,2,2,' var pos = fromStr.indexOf(findStr); // Count the end position requested return pos >= 0 ? fromStr.slice(0,pos+1).match(/,/g).length + needle.length - 1 : -1; Note: The comma at head and tail is to make sure [22,12] doesn't match [2,1].