<p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>&lfloor; n/3 &rfloor;</code> times.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
      //1.

        // vector<int>vec;
        // int n = nums.size();
        // map<int,int>mp;
        // for(int i = 0; i<n; i++){
        //     mp[nums[i]]++;
        // }
        // for(auto it:mp){
        //     if(it.second>n/3){
        //         vec.push_back(it.first);
        //     }
        // }
        // return vec;

    //2. better use of map;

    //     int n = nums.size(); //size of the array
    //     vector<int> ls; // list of answers

    // //declaring a map:
    //     map<int, int> mpp;

    // // least occurrence of the majority element:
    //     int mini = int(n / 3) + 1;

    // //storing the elements with its occurnce:
    //    for (int i = 0; i < n; i++) {
    //      mpp[nums[i]]++;

    //       //checking if v[i] is
    //       // the majority element:
    //      if (mpp[nums[i]] == mini) {
    //         ls.push_back(nums[i]);
    //      }
    //      if (ls.size() == 2) break;
    //     }

    //     return ls;


    //3. Optimal approach(to solve this problem in o(1))- Moore Voting Algorithm
        vector<int>vec;
        int candidate1 = 0;
        int candidate2 = 0;
        int count1 = 0;
        int count2 = 0;
        for(int i = 0; i<nums.size(); i++){
            if(nums[i]==candidate1){
                count1++;
            }
            else if(nums[i]==candidate2){
                count2++;
            }
            else if(count1==0 && nums[i]!=candidate2){
                candidate1 = nums[i];
                count1++;
            }
            else if(count2==0 && nums[i]!=candidate1){
                candidate2 = nums[i];
                count2++;
            }
            else{
                count1--;
                count2--;
            }
        }

       int cnt1 = 0;
       int cnt2 = 0;
       for(int i = 0; i<nums.size(); i++){
          if(nums[i]==candidate1) cnt1++;
           else if(nums[i]==candidate2){
               cnt2++;
            } 
        }
       if(cnt1>nums.size()/3) vec.push_back(candidate1);
       if(cnt2>nums.size()/3) vec.push_back(candidate2);
       return vec;
    }
};
18500