-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathaverage_mode.jl
41 lines (35 loc) · 1.08 KB
/
average_mode.jl
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
"""
mode(nums)
Finds mode of a vector of numbers
# Example
```julia
mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]) # returns [2]
mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]) # returns [2]
mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]) # returns [2, 4]
mode(["x", "y", "y", "z"]) # returns ["y"]
mode(["x", "x" , "y", "y", "z"]) # returns ["x", "y"]
```
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee)
"""
function mode(nums)
dict = Dict() # nums => Number of repetitions
result = [] # Array of the modes so far
max = 0 # Max of repetitions so far
for i in nums
# Add one to the dict[i] entry (create one if none)
if i in keys(dict)
dict[i] += 1
else
dict[i] = 1
end
# Result updated if no of repetitions of i >= max
if dict[i] >= max
if dict[i] > max
empty!(result)
max += 1
end
append!(result, [i])
end
end
return result
end