Question: Write a standalone function outside of the DynamicArray class that receives a dynamic array already in sorted order, either non-descending or non-ascending. The function
Write a standalone function outside of the DynamicArray class that receives a dynamic array already in sorted order, either non-descending or non-ascending. The function will return a tuple containing (in this order) a dynamic array comprising the mode (most-occurring) value/s of the array, and an integer that represents the highest frequency (how many times they appear). If there is more than one value that has the highest frequency, all values at that frequency should be included in the array being returned in the order in which they appear in the input array. If there is only one mode, only that value should be included. You may assume that the input array will contain one or more homogeneous elements(either all numbers, or strings, or custom objects, but never a mix of these). You do not need to write checks for these conditions. For full credit, the function must be implemented with O(N) complexity with no additional data structures (beyond the array you return) being created. (Note: You can replace the return array as needed) Example #1: test cases ( > [1, 1, 2, 3, 3, 4], [1, 2, 3, 4, 51, ["Apple", "Banana", "Banana", "Carrot", "Carrot", "Date", "Date", "Date", "Eggplant", "Eggplant", "Eggplant", "Fig", "Fig", "Grape"] for case in test cases: da DynamicArray (case) mode, frequency find_mode (da) print (f" (da) Mode: (mode), Frequency: (frequency} ") case 14, 3, 3, 2, 2, 2, 1, 1, 1, 11 da DynamicArray() for x in range (len (case)): da.append(case [x]) mode, frequency = find_mode (da) print (f" (da) Mode: (mode), Frequency: (frequency} "); Output: DYN ARR Size/Cap: 6/8 [1, 1, 2, 3, 3, 41 Mode: DYN ARR Size/Cap: 2/4 [1, 31, Frequency: 2 DYN ARR Size/Cap: 5/8 [1, 2, 3, 4, 51 Mode: DYN ARR Size/Cap: 5/8 [1, 2, 3, 4, 51, Frequency: 1
Step by Step Solution
There are 3 Steps involved in it
To solve the given problem of finding the modes in a DynamicArray while maintaining ON complexity and using no additional data structures we need to iterate over the array efficiently while keeping tr... View full answer
Get step-by-step solutions from verified subject matter experts
