Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript find repeated character

Input:
S = "geeksforgeeks"
Output: g
Explanation: g, e, k and s are the repeating
characters. Out of these, g occurs first.


const repeatedCharacter = (str) => {
  for (let i = 0; i < str.length - 1; i++) {
    for (let j = i + 1; j < str.length; j++) {
      if (str[i] === str[j]) {
        return str[i];
      }
    }
  }
  return -1
};
console.log(repeatedCharacter("geeksforgeeks"));

Comment

how to find repeated characters in a string in javascript

/* Time complexity of solution: O(n) */
const getRepeatedChars = (str) => {
 	const chars = {};
    for (const char of str) {
        chars[char] = (chars[char] || 0) + 1;
    }
    return Object.entries(chars).filter(char => char[1] > 1).map(char => char[0]);
}

getRepeatedChars("aabbkdndiccoekdczufnrz"); // ["a", "b", "c", "d", "k", "n", "z"]
Comment

javascript check repeated char

function hasRepeats (str) {
    return /(.).*1/.test(str);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript check if object 
Javascript :: npx http server 
Javascript :: sticky footer react 
Javascript :: anime js link 
Javascript :: laravel link custom javascript file 
Javascript :: how to access router from the store vue 
Javascript :: how to move div using jquery 
Javascript :: javascript option yes/no popup 
Javascript :: How to replace a value in localstorage using javascript 
Javascript :: JavaScript Object Constructors 
Javascript :: js substring first 4 numbwe 
Javascript :: column width table react 
Javascript :: number_format in jquery 
Javascript :: await on observable 
Javascript :: how to get thumbnail image from video file in javascript 
Javascript :: redondear decimales javascript 
Javascript :: play sound with keydown javascript 
Javascript :: js platformer 
Javascript :: do while javascript 
Javascript :: last index of javascript 
Javascript :: string to json js 
Javascript :: useNavigate history back 
Javascript :: how to save and use item from local storage javascript 
Javascript :: javascript click counter 
Javascript :: count down timer in react native 
Javascript :: js join 
Javascript :: checkbox jquery checked 
Javascript :: iterate array in javascrpt 
Javascript :: convert currency to string javascript 
Javascript :: path resolve in node js to current dir 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =