Consider the task of finding the most frequent element in an array of length n. Here are
Question:
Consider the task of finding the most frequent element in an array of length n. Here are three approaches:
a. Sort the array, then find the longest run.
b. Allocate an array of counters of the same size as the original array. For each element, traverse the array and count how many other elements are equal to it, updating its counter. Then find the maximum count.
c. Keep variables for the most frequent element that you have seen so far and its frequency. For each index i, check whether a[i] occurs in a[0] ... a[i - 1]. If not, count how often it occurs in a[i + 1] ... a[n - 1]. If a[i] is more frequent than the most frequent element so far, update the variables. Describe the big-Oh efficiency of each approach.
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Related Book For
Question Posted: