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

duplicate characters in a string javascript

const text = 'abcda'.split('')
text.some( (v, i, a) => {
  return a.lastIndexOf(v) !== i
}) // true
Comment

PREVIOUS NEXT
Code Example
Javascript :: queen of spain 
Javascript :: function example 
Javascript :: create serverless hello-world 
Javascript :: video playing 
Javascript :: Make stepper with 2 values javascript 
Javascript :: html how to get js 
Javascript :: verificar radio selected 
Javascript :: blank array condition in react js 
Javascript :: map for id 
Javascript :: how to remove tashkeel from arabic charactor 
Javascript :: vue-jstree 
Javascript :: call apply mnemonic javascript 
Javascript :: uppy count files from javascript 
Javascript :: highlight each occurrence of text 
Javascript :: Event listener with single mouse click in extendscript 
Javascript :: jquery override page title 
Javascript :: swapping java primitives values 
Javascript :: Scrub punctuation character 
Javascript :: How to set variable data in JSON body for the code that generated by Postman in c# 
Javascript :: bug in javascript 
Javascript :: google sheets simulate edit event 
Javascript :: how to check length checkbox has been checked 
Javascript :: localStorage check 
Javascript :: javascript random to abs 
Javascript :: redux how does dispatch know which reducer to call 
Javascript :: javascript babel run 
Javascript :: cache management in angular 7 
Javascript :: update instance in sequelize 
Javascript :: javascript auto detect if select input changed 
Javascript :: .localecompare number func 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =