Untitled

 avatar
unknown
plain_text
5 months ago
1.6 kB
3
Indexable
import java.util.*;

public class MajorityElementII {
    public static List<Integer> majorityElement(int[] nums) {
        int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;
        
        // Step 1: Identify potential candidates
        for (int num : nums) {
            if (num == candidate1) {
                count1++;
            } else if (num == candidate2) {
                count2++;
            } else if (count1 == 0) {
                candidate1 = num;
                count1 = 1;
            } else if (count2 == 0) {
                candidate2 = num;
                count2 = 1;
            } else {
                count1--;
                count2--;
            }
        }
        
        // Step 2: Verify the candidates
        count1 = 0;
        count2 = 0;
        for (int num : nums) {
            if (num == candidate1) count1++;
            else if (num == candidate2) count2++;
        }
        
        List<Integer> result = new ArrayList<>();
        if (count1 > nums.length / 3) result.add(candidate1);
        if (count2 > nums.length / 3) result.add(candidate2);
        return result;
    }

    public static void main(String[] args) {
        int[] nums1 = {3, 2, 3};
        System.out.println(majorityElement(nums1)); // Output: [3]

        int[] nums2 = {1, 1, 1, 3, 3, 2, 2, 2};
        System.out.println(majorityElement(nums2)); // Output: [1, 2]

        int[] nums3 = {1, 2, 3};
        System.out.println(majorityElement(nums3)); // Output: []
    }
}
Editor is loading...
Leave a Comment