File tree Expand file tree Collapse file tree 4 files changed +92
-0
lines changed
solution/1600-1699/1636.Sort Array by Increasing Frequency Expand file tree Collapse file tree 4 files changed +92
-0
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,40 @@ func frequencySort(nums []int) []int {
125
125
}
126
126
```
127
127
128
+ ### ** TypeScript**
129
+
130
+ ``` ts
131
+ function frequencySort(nums : number []): number [] {
132
+ const map = new Map <number , number >();
133
+ for (const num of nums ) {
134
+ map .set (num , (map .get (num ) ?? 0 ) + 1 );
135
+ }
136
+ return nums .sort ((a , b ) => map .get (a ) - map .get (b ) || b - a );
137
+ }
138
+ ```
139
+
140
+ ### ** Rust**
141
+
142
+ ``` rust
143
+ use std :: collections :: HashMap ;
144
+ impl Solution {
145
+ pub fn frequency_sort (mut nums : Vec <i32 >) -> Vec <i32 > {
146
+ let n = nums . len ();
147
+ let mut map = HashMap :: new ();
148
+ for & num in nums . iter () {
149
+ * map . entry (num ). or_insert (0 ) += 1 ;
150
+ }
151
+ nums . sort_by (| a , b | {
152
+ if map . get (a ) == map . get (b ) {
153
+ return b . cmp (a );
154
+ }
155
+ map . get (a ). cmp (& map . get (b ))
156
+ });
157
+ nums
158
+ }
159
+ }
160
+ ```
161
+
128
162
### ** ...**
129
163
130
164
```
Original file line number Diff line number Diff line change @@ -110,6 +110,40 @@ func frequencySort(nums []int) []int {
110
110
}
111
111
```
112
112
113
+ ### ** TypeScript**
114
+
115
+ ``` ts
116
+ function frequencySort(nums : number []): number [] {
117
+ const map = new Map <number , number >();
118
+ for (const num of nums ) {
119
+ map .set (num , (map .get (num ) ?? 0 ) + 1 );
120
+ }
121
+ return nums .sort ((a , b ) => map .get (a ) - map .get (b ) || b - a );
122
+ }
123
+ ```
124
+
125
+ ### ** Rust**
126
+
127
+ ``` rust
128
+ use std :: collections :: HashMap ;
129
+ impl Solution {
130
+ pub fn frequency_sort (mut nums : Vec <i32 >) -> Vec <i32 > {
131
+ let n = nums . len ();
132
+ let mut map = HashMap :: new ();
133
+ for & num in nums . iter () {
134
+ * map . entry (num ). or_insert (0 ) += 1 ;
135
+ }
136
+ nums . sort_by (| a , b | {
137
+ if map . get (a ) == map . get (b ) {
138
+ return b . cmp (a );
139
+ }
140
+ map . get (a ). cmp (& map . get (b ))
141
+ });
142
+ nums
143
+ }
144
+ }
145
+ ```
146
+
113
147
### ** ...**
114
148
115
149
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+ impl Solution {
3
+ pub fn frequency_sort ( mut nums : Vec < i32 > ) -> Vec < i32 > {
4
+ let n = nums. len ( ) ;
5
+ let mut map = HashMap :: new ( ) ;
6
+ for & num in nums. iter ( ) {
7
+ * map. entry ( num) . or_insert ( 0 ) += 1 ;
8
+ }
9
+ nums. sort_by ( |a, b| {
10
+ if map. get ( a) == map. get ( b) {
11
+ return b. cmp ( a) ;
12
+ }
13
+ map. get ( a) . cmp ( & map. get ( b) )
14
+ } ) ;
15
+ nums
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ function frequencySort ( nums : number [ ] ) : number [ ] {
2
+ const map = new Map < number , number > ( ) ;
3
+ for ( const num of nums ) {
4
+ map . set ( num , ( map . get ( num ) ?? 0 ) + 1 ) ;
5
+ }
6
+ return nums . sort ( ( a , b ) => map . get ( a ) - map . get ( b ) || b - a ) ;
7
+ }
You can’t perform that action at this time.
0 commit comments