Americas
Europe
Q14E
Expert-verifiedGive a recursive algorithm for finding a mode of a list of integers. (A mode is an element in the list that occurs at least as often as every other element.)
The recursive algorithm is,
The objective is to write the recursive algorithm for finding a mode of a list of integers.
Call the algorithm "mod" and the input is a list of integers.
When the list contains only 1 integer, then the mode is the remaining integer.
If the list contains more than 1 value, then check, if the last value in the list is repeated somewhere in the list. Remove this value along with all other repetitions of the value form the list, while represents the number of values that were deleted. If , then all elements in the list were deleted and therefore, there should be return of . Otherwise, apply the algorithm to determine the mode of the reduced list,
which occurs times. If k > m , then is the mode. If k < m, then the mode is the mode of the reduced list.else
And,
By combining all the steps, the required algorithm will be as follows.
Therefore, the recursive algorithm is shown above.
94% of StudySmarter users get better grades.
Sign up for free