Question: find _ mode ( arr: DynamicArray ) - > tuple [ DynamicArray , int ] : Write a standalone function outside of the HashMap class

find_mode(arr: DynamicArray)-> tuple[DynamicArray, int]:
Write a standalone function outside of the HashMap class that receives a dynamic array,
which is not guaranteed to be sorted. This function will return a tuple containing, in this
order, a dynamic array comprising the mode (most occurring) value(s) of the given array,
and an integer representing the highest frequency of occurrence for the mode value(s).
If there is more than one value with the highest frequency, all values at that frequency
should be included in the array being returned (the order does not matter). If there is only
one mode, the dynamic array will only contain that value.
You may assume that the input array will contain at least one element, and that all values
stored in the array will be strings. You do not need to write checks for these conditions.
For full credit, the function must be implemented with O(N) time complexity. For best
results, we recommend using the separate chaining hash map instance provided for you in
the functions skeleton code.
Example #1:
da = DynamicArray(["apple", "apple", "grape", "melon", "peach"])
mode, frequency = find_mode(da)
print(f"Input: {da}
Mode : {mode}, Frequency: {frequency}")
Output:
Input: ['apple', 'apple', 'grape', 'melon', 'peach']
Mode : ['apple'], Frequency: 2
Example #2:
test_cases =(
["Arch", "Manjaro", "Manjaro", "Mint", "Mint", "Mint", "Ubuntu",
"Ubuntu", "Ubuntu"],
["one", "two", "three", "four", "five"],
["2","4","2","6","8","4","1","3","4","5","7","3","3","2"]
)
for case in test_cases:
da = DynamicArray(case)
mode, frequency = find_mode(da)
print(f"{da}
Mode : {mode}, Frequency: {frequency}
")
Page 16 of 29

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!