• edited by
836 views
0 0 votes

What is the running time for an algorithm which computes the most frequently occurring element of an array $A[1 \ldots n]$ ?

  1. $\mathrm{O}(\log \mathrm{n})$
  2. $\mathrm{O}(\mathrm{n})$
  3. $O(n \log n)$
  4. $\mathrm{O}\left(\mathrm{n}^{2}\right)$

1 Answer

1 1 vote

Assuming an array to be unsorted ,

we can maintain a count of occurence of each element in an other array , say count[] .We read an element from input array and increment that index of count[] which is the value in the input array.

So we get count of each element in 1 iteration over the input array.

Next by getting the maximum count from count array ,we can report the corresponding index of count array which is actually the element which is occuring most frequently.

Hence the correct answer should be B)

Position:
Show:

Related questions

9 9 votes
2 answers 2 answers
23.4k
23.4k views
0 0 votes
2 answers 2 answers
2.2k
2.2k views
venky.victory35 asked Dec 19, 2015
2,217 views
We are given a sequence of $n$ positive numbers $a_{1}, a_{2}, \ldots, a_{n}$ and a fixed number $k>0$. We want to find a pair of numbers $a_{i}$ and $a_{j}$ such that $j...
2 2 votes
1 1 answer
2.2k
2.2k views
yes asked Oct 6, 2015
2,215 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)