find the majority element

    1. Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
      1. If the counter is 0, we set the current candidate to x and the counter to 1.
      2. If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
      After one pass, the current candidate is the majority element. Runtime complexity = O(n).
posted @ 2014-12-24 12:50  zmiao  阅读(151)  评论(0编辑  收藏  举报