1

I wanted to ask if there is an algorithm that can find, in an array of length n, if there is an array element with a certain frequency percentage (10%, 20% etc...) in linear time.

Selection sort is O(n^2) and the majority algorithm even if O(n) can only find if the number is repeated at least n/2 times.

6
  • 2
    just iterate over the array, count occurences and divide by the number of elements? Commented Jul 22, 2014 at 7:46
  • In O(n)? If i count the occurrences of every element I have to iterate over the array n times so complexity will be n^2, i thought about something like counting sort but the interval of numbers is not known Commented Jul 22, 2014 at 7:58
  • if you apply count sort then only one time iteration will be done means complexity is O(n) and getting count of that number is in O(1) Commented Jul 22, 2014 at 8:02
  • 2
    Are there restrictions on space usage? You can simply create a dictionary mapping elements to count. This takes an additional O(n) space for an algorithm that runs in O(n) time. Commented Jul 22, 2014 at 8:15
  • 1
    Why do a radix sort at all? Just create a dictionary. If that's a bit more spacious than necessary, who cares? Create dict, iterate list, increase counters, iterate dict to find frequent numbers. Commented Jul 22, 2014 at 9:10

2 Answers 2

1

You are making this out to be a lot harder than it really is, assuming you don't have space limits.

The idea is to iterate the array and save the count of each element while doing so. Dictionary (or equivalent structure) lookup is O(1) so it's not a problem.
Then just iterate over the dictionary and see what elements have the required frequency.

Python pseudocode:

for elem in array:
    count[elem] = count.get(elem, 0) + 1
for elem, elem_count in count.items():
    if 0.20 <= float(elem_count) / len(array) <= 0.25:
        print "{} has a frequency between 20% and 25%".format(elem)
Sign up to request clarification or add additional context in comments.

Comments

0

int counter = 0; (no influence on run time in this code line).
Iterate the arry using loop (for,while,whatever). For each element:
- if(element == what_you_search)
counter++;

//This if statment is o(1) in each iteration, there were n interation so in the overall it is
//o(n) for the whole loop.
int percentage = counter / (array.len()); //It is being done in o(1).

o(1) + o(n) + o(1) = o(n)

4 Comments

Please be careful with notation: o(n) is not the same as O(n).
Ohh misunderstood question.... in that case there is a diffent algorithm. don't remmember it's name.
I will write here pseudo code: SortedArray = Array accepted from any O(n) sorted array algorithm. (I know there are few like this just choose one). CheckElement = sortedArray[0] Counter = 1 for(int i = 1; i< array.len ; i++){ if(SortedArray[i] == CheckElement) Counter++; else{ if counter indicates elements shows 10% or whatever return true; else CheckElement = SortedArray[i]; } }
General idea is sorting array and then check how many equal values are repetead until get an index having a new element value, then see if counter of elements is ok for your criteria

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.