1function removeDuplicateCharacters(string) {
2 return string
3 .split('')
4 .filter(function(item, pos, self) {
5 return self.indexOf(item) == pos;
6 })
7 .join('');
8}
9console.log(removeDuplicateCharacters('baraban'));
1String arr[] = {"12341234abc", "1234foo1234", "12121212", "111111111", "1a1212b123123c12341234d1234512345"};
2String regex = "(\\d+?)\\1";
3Pattern p = Pattern.compile(regex);
4for (String elem : arr) {
5 boolean noMatchFound = true;
6 Matcher matcher = p.matcher(elem);
7 while (matcher.find()) {
8 noMatchFound = false;
9 System.out.println(elem + " got repeated: " + matcher.group(1));
10 }
11 if (noMatchFound) {
12 System.out.println(elem + " has no repeation");
13 }
14}
15
1/* Time complexity of solution: O(n) */
2const getRepeatedChars = (str) => {
3 const chars = {};
4 for (const char of str) {
5 chars[char] = (chars[char] || 0) + 1;
6 }
7 return Object.entries(chars).filter(char => char[1] > 1).map(char => char[0]);
8}
9
10getRepeatedChars("aabbkdndiccoekdczufnrz"); // ["a", "b", "c", "d", "k", "n", "z"]
1const text = 'abcda'.split('')
2text.some( (v, i, a) => {
3 return a.lastIndexOf(v) !== i
4}) // true
1const getRepeatedChars = (str) => {
2 let result = [];
3 str.map(each => {
4 let repeatedChars = 0;
5 for (let i = 0; i < each.length - 1; i++) {
6 if (each[i] === each[i + 1] && each[i] !== each[i - 1]) {
7 repeatedChars += 1;
8 }
9 }
10
11 result.push(repeatedChars);
12 });
13
14 return result;
15};
16
17getRepeatedChars(["aaabbbkdnndicccoekdczufnrz", "awsfgds"]); // [4, 0]