-
Notifications
You must be signed in to change notification settings - Fork 15
/
SortByFrequency.js
33 lines (29 loc) · 871 Bytes
/
SortByFrequency.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
/*
Asked By - Zycus, Oracle.
Given an array of integers, sort the array according to frequency of elements.
For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12},
then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.
If frequencies of two elements are same, print them in increasing order.
*/
const sortByFrequency = (array) => {
let frequency = []
let Sort = []
let result = []
array.forEach((value) => {
if(value in frequency)
frequency[value]++
else
frequency[value] = 1
})
for(let key in frequency)
Sort.push([key, frequency[key]])
Sort.sort((a,b) => b[1] - a[1])
Sort.forEach((obj) => {
for(let i=0;i<obj[1];i++)
result.push(obj[0])
})
return result
}
const array = [5,5,4,6,4]
sortByFrequency(array)
// Output - [4,4,5,5,6]