-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcharacterFrequency.ts
More file actions
49 lines (44 loc) · 923 Bytes
/
characterFrequency.ts
File metadata and controls
49 lines (44 loc) · 923 Bytes
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
/*
* Write a function that takes as its input a string and returns an array of
* arrays as shown below sorted in descending order by frequency and then by
* ascending order by character.
1.
*
*
* :: Example ::
*
* characterFrequency('mississippi') ===
* [
* ['i', 4],
* ['s', 4],
* ['p', 2],
* ['m', 1]
* ]
*
* :: Gotcha ::
*
* characterFrequency('miaaiaaippi') ===
* [
* ['a', 4],
* ['i', 4],
* ['p', 2],
* ['m', 1]
* ]
*
*
*/
var characterFrequency = function(string: string): Array<Array<string>> {
var result = [];
for (var i = 0; i < string.length; i++) {
var array = [];
array.push(string[i]);
for (var j = 0; j < array.length; j++) {
}
if (result[i] !== string[i]){
result.push(string[i]);
}
result.push(string[i]);
}
return result;
};
console.log(characterFrequency('mississippi'));