-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate.js
More file actions
57 lines (50 loc) · 1.57 KB
/
Copy pathduplicate.js
File metadata and controls
57 lines (50 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// How do you print duplicate characters from a string
//STEPS
//VERIFY INPUTS/PROBLEM
//Input must be a string, return null for empty string or not string.
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION
//iterate over the string and keep a count of each string, store in a dictionary or hash map.
//if a character exists in the hash map it means it's a duplicate string, and used be added to a new string.
//WRITE OUT THE FIRST VERSION
function printDuplicatecharcaters (str){
if(typeof str !== 'string' || str === ''){
return null;
}
let charCount = {};
let duplicateChars ='';
for (char of str){
console.log('times')
if (charCount[char]){
duplicateChars +=char;
} else {
charCount[char] = 1;
}
}
return duplicateChars;
}
//VERIFY RESULTS
//Test the code with possible string input
console.log(printDuplicatecharcaters('MercyOgbenjuwaFatimahOnyoibo'))
console.log(printDuplicatecharcaters('JesusBaby'))
console.log(printDuplicatecharcaters('WalkOFFaith'))
//DERIVE THE TIME COMPLEXITY
//Based on the input size 0(n) - Linear time Complexity
//BEST CASE => JesusBaby,
//AVERAGE CASE => WalkOfFaith,
//WORST CASE => MercyOgbenjuwaFatimahOnyoibo
//EXPLORE ALTERNATIVES
function printDuplicatecharcaters (str) {
if (typeof str !== 'str' || str === ''){
return null;
}
let charCount = new Set();
let duplicateChars = [];
for (char of str){
if(charCount.has(char)){
duplicateChars.push(char);
}else{
charCount.add(char);
}
}
return duplicateChars;
}