diff --git a/6_kyu/Only Duplicates/README.md b/6_kyu/Only Duplicates/README.md new file mode 100644 index 0000000..8f99e53 --- /dev/null +++ b/6_kyu/Only Duplicates/README.md @@ -0,0 +1,11 @@ +## Only Duplicates + +https://www.codewars.com/kata/5a1dc4baffe75f270200006b + +Given a string, remove any characters that are unique from the string. + +Example: + +input: "abccdefee" + +output: "cceee" diff --git a/6_kyu/Only Duplicates/index.test.ts b/6_kyu/Only Duplicates/index.test.ts new file mode 100644 index 0000000..8ab3509 --- /dev/null +++ b/6_kyu/Only Duplicates/index.test.ts @@ -0,0 +1,10 @@ +import { onlyDuplicates } from "./index"; + +describe("Tests", () => { + it("example", () => { + expect(onlyDuplicates("abccdefee")).toBe("cceee"); + expect(onlyDuplicates("hello")).toBe("ll"); + expect(onlyDuplicates("colloquial")).toBe("ollol"); + expect(onlyDuplicates("foundersandcoders")).toBe("ondersndoders"); + }); +}); diff --git a/6_kyu/Only Duplicates/index.ts b/6_kyu/Only Duplicates/index.ts new file mode 100644 index 0000000..7cfe8c4 --- /dev/null +++ b/6_kyu/Only Duplicates/index.ts @@ -0,0 +1,16 @@ +export function onlyDuplicates(str: string): string { + const array: string[] = str.split(""); + const hashTable: { [key: string]: number } = {}; + + array.forEach((item) => { + if (hashTable[item]) { + hashTable[item]++; + } else { + hashTable[item] = 1; + } + }); + + const dublicates = array.filter((item) => hashTable[item] > 1); + + return dublicates.join(""); +} diff --git a/README.md b/README.md index fcf0908..38d4571 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ ### Katas solved -`Total`: 115 \ +`Total`: 116 \ `8_kyu`: 92 \ `7_kyu`: 15 \ -`6_kyu`: 8 \ +`6_kyu`: 9 \ `5_kyu`: 0 \ `4_kyu`: 0 \ `3_kyu`: 0 \