-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg111.js
52 lines (46 loc) · 1.2 KB
/
pg111.js
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
//Remove duplicate charaters (case sensitive) including punctuation. Keep only the LAST instance of each character.
var givenDedupe = "Snaps! crackles! pops!" //return: Snrackle ops!
var dedupe = function(str){
var charDict = {}
for(var i=0; i<str.length; i++){
if(charDict.hasOwnProperty(str[i])){
charDict[str[i]].push(i);
}
else{
charDict[str[i]] = [i];
}
}
for(var key in charDict){
charDict[key] = charDict[key].splice(charDict[key].length-1);
};
var newArr = []
for(var key in charDict){
newArr[charDict[key]] = key;
};
return newArr.join('');
}
// console.log(dedupe(givenDedupe));
//Return the index of the first unique (case-sensitive) character in a given string.
var givenUnique = "empathetic monarch meets primo stinker" //return: 35 (k)
var unique = function(str){
var charDict = {}
for(var i=0; i<str.length; i++){
if(charDict.hasOwnProperty(str[i])){
charDict[str[i]].push(i);
}
else{
charDict[str[i]] = [i];
};
};
uniquePos = [];
for(var key in charDict){
if(charDict[key].length>1){
delete charDict[key];
continue;
};
uniquePos.push(charDict[key][0]);
};
uniquePos = uniquePos.sort();
return uniquePos[0];
};
console.log(unique(givenUnique))