Java Program to Find an Index of an Array Such That Its Value Occurs at More Than Half of Indices in The Array26 Mar 2025 | 3 min read Problem StatementFinding an index in an array such that the value at that index appears at more than half of the array's indices is the task at hand. This number is frequently referred to as the array's majority element. An element that occurs more than n/2 times, where n is the array's length, is considered the majority element in the array. Return the index of one such occurrence if such an element is present; else, return -1 if there is no majority element. ExampleInput: Output: 0 Explanation An array element 2 appears 4 times out of 6 indices. Since 4 > 6/2 (which is 3), 2 is the majority element. The function should return any index of 2. In this case, the index 0 is one valid answer. Other valid answers could be 2, 3, or 5. Solution to the ApproachThe Boyer-Moore Voting Algorithm is an optimal solution with a time complexity of O(n) and a space complexity of O(1). The algorithm is based on the following observations:
ExampleOutput: Index of majority element: 0 ExplanationThe Java code provided efficiently finds an index of an array where the value at that index is the majority element, meaning it appears more than half the time in the array. The findMajorityElementIndex method serves as the primary function and calls two helper methods: findCandidate and isMajority. The findCandidate method uses the Boyer-Moore Voting Algorithm to select a potential majority element by iterating through the array and adjusting a candidate and its count. If the current count is zero, it sets a new candidate; otherwise, it increases or decreases the count based on whether the current element matches the candidate. Once a candidate is identified, the isMajority method verifies whether this candidate truly is a majority element by counting its occurrences in the array. If the candidate's count exceeds half the length of the array, it is confirmed as the majority element, and the findMajorityElementIndex method returns the first index of this candidate. If no majority element exists, it returns -1. This method works best, assuring efficient performance for massive arrays with an O(n) time complexity and an O(1) space complexity. ConclusionThe code above offers an effective method of utilizing the Boyer-Moore Voting Algorithm to discover the index of an array element that appears more than half of the time in the array; this solution runs in O(n) time with O(1) space complexity, making it ideal for this application. Next TopicTop 10 Java Books |
One array containing non-negative numbers is given to us. Also, a number K is given. Our task is to count the number of pairs from the given array such that the OR operation between the elements in the pair gives a value greater than K. Example 1: Input int...
6 min read
The java.text.ChoiceFormat is a class containing a parse() as a function. To retrieve the limit value for a specific format in a ChoiceFormat object, utilize the ChoiceFormat class. Syntax: public Number parse(String text_name, ParsePosition status_Of_choice) Parameters: - where the parameters that this method takes is text_name: which is the text...
3 min read
The topic mainly concerns those programmers or developers who wish to deal with Java programming language on Windows XP or Windows Vista. This section will discuss Windows programming using Java and other detailed information related to the concept. What is Windows Programming Although the answer to this question always...
5 min read
An integer array (or array list) is given to us. Our task is to print all the subsets of the given integer array (excluding empty subsets). Note that the order in which subsets are displayed does not matter. Example 1: Input int inputArr[] = {1, 2, 3} Output: {1}, {2},...
7 min read
A vertex in a graph that is the starting point for all other vertices to reach is known as the mother vertex. Stated otherwise, if vertex v is a mother vertex, then a path connects v to every other vertex in the network. Finding a mother...
6 min read
The java.time.chrono.ThaiBuddhistDate is a class containing a now() as a function. The ThaiBuddhistDate class is used to retrieve the current ThaiBuddhist date from the given clock that is compatible with the ThaiBuddhist calendar system. Syntax: public static ThaiBuddhistDate now(Clock clock) Parameter: In accordance with this method, the Thai...
2 min read
In Java, the @SuppressWarnings is defined as an annotation that is utilized for suppressing or ignoring particular warnings which are raised by the compiler because of a specific code. In simple words, the compiler gets indicated by the @SuppressWarnings annotation to pass over or ignore particular...
4 min read
In this section, we learn how to compile and run java program step by step. Step 1: Write a program on the notepad and save it with .java (for example, DemoFile.java) extension. class DemoFile { public static void main(String args[]) { System.out.println("Hello!"); System.out.println("Java"); } } Step 2: Open Command Prompt. Step 3: Set the directory in which the .java...
1 min read
Validating the input is frequently required in many applications to ensure it only contains numbers. Processing user input, verifying data formats, or guaranteeing that strings represent actual numeric values can be helpful. We may use various techniques in Java, including regular expressions, streams, and basic iteration,...
5 min read
is an array that holds character data types values. In Java programming, unlike C, a character array is different from a string array, and neither a string nor a character array can be terminated by the NULL character. The Java language uses UTF-16 representation in...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India