Computing the statistical mode

余生颓废 提交于 2019-12-13 12:14:42

问题


I'm currently trying to verify whether or not, given an unsorted array A of length N and an integer k, whether there exists some element that occurs n/k times or more.

My thinking for this problem was to compute the mode and then compare this to n/k. However, I don't know how to compute this mode quickly. My final result needs to be nlog(k), but I have no idea really on how to do this. The quickest I could find was nk...


回答1:


Use a hash table to count the frequency of each value:

uint[int] counts;
foreach(num; myArray) {
     counts[num]++;
}

int mostFrequent;
uint maxCount = 0;
foreach(num, count; counts) {
    if(count > maxCount) { 
        mostFrequent = num;
        maxCount = count;
    }
}



回答2:


Set m = n/k rounded up. Do a quicksort, but discard sublists of length less than m.

Like quicksort, you can have bad luck and repeatedly choose pivots that close to the ends. But this has a small probability of happening, if you choose the pivots randomly.

There'll be O(log(k)) levels to the recursion, and each level takes O(n) time.




回答3:


Just walking the array and keeping counts in a hash/dictionary (and returning true once n/k is found, else false) would be O(n)

edit, something like:

counts = {}
for n in numbers:
    if ( counts.has_key( n ) ):
        counts[ n ] += 1
    else:
        counts[ n ] = 1
    if ( counts[ n ] >= n / k ):
        return true
return false



回答4:


Calculating Statistical Mode in F# .net for data set (integers) that has single Mode

let foundX (num: int, dList) = List.filter (fun x -> x = num) dList
let groupNum dList =
    dList
    |> (List.map (fun x -> foundX (x, dList)))
    |> (List.maxBy (fun x -> x.Length))

let Mode (dList: int List) = 
    let x = groupNum dList
    x.Head

//using Mode
let data = [1;1;1;1;1;1;1;1;2;2;3;3;3;1;4;4;4;4;4]
Mode data;;`




回答5:


Pseudocode:

 found = false
 value = null
 B = new hashtable
 for (i =0, j = A[i]; i < |A| and !found; ++i, j=A[i])
    if B contains key j
       B[j] = B[j] + 1
       if B[j] > |A|/k
          found = true
          value = j
       endif
    else 
       B[j] = 1
    endif
 end for

Assuming that your hashtable implementation has O(1) insert/lookup this should be O(n)



来源:https://stackoverflow.com/questions/512590/computing-the-statistical-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!