Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:16:03+00:00 2026-05-28T18:16:03+00:00

Dominator is a value which occurs on more than half positions in an array.

  • 0

Dominator is a value which occurs on more than half positions in an array. For instance in the array:
[3, 4, 3, 2, 3, -1, 3, 3]
the value 3 occurs in 5 out of 8 positions. 5/8 > 0.5,
so 3 is a dominator:
in this array the dominator occurs on indexes :
0, 2, 4, 6 , 7.
How to write a function that given an array returns any index on which its dominator occurs. If there is no dominator, the function should return -1.

Tried to solve it using C as:

int arrdominator(int *A,int N)
{
    int i,j,idx=0,val=0,cnt=1;

    //I thought I would sort the array and then cound the dominating value instances,
    qsort(A,N,sizeof(A[0]),sort_fn_ascend);

    for(i=0;i<(N-1);i++)
    {
        for(j=i+1;j<N;j++)
        {
            if(A[i] == A[j])
            {
                cnt++;

                i++;
                break;
            }
        }
    }
}

But i could not get to a solution of finding the dominator value in the array and then return its index.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:16:04+00:00Added an answer on May 28, 2026 at 6:16 pm

    Thanks to @DocBrown’s pointers got my initial code refined to get it working alright:

    int arrdominator(int *A,int N)
    {
        int i,j,idx=0,cnt=1,valmaxseq,dominantval=0;
        int *newA = (int*)malloc(sizeof(int)*N);
        for(i=0;i<N;i++)
            newA[i] = A[i];
    
        qsort(newA,N,sizeof(A[0]),sort_fn_ascend);
    
    
        for(j=0;j<(N-1);j++)
        {
            if(newA[j] == newA[j+1])
            {
                cnt++;              
            }
            else
            {               
                if(cnt > (N/2))
                {                   
                    valmaxseq = newA[j];
                    dominantval = 1;
                }
    
                cnt = 1;                
            }
        }
    
            //find index of dominant value if there is one
            if(dominantval == 1)
            for(i=0;i<N;i++)
            {
                if(A[i] == valmaxseq)
                    return i;
            }
    
            return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes which represent a special kind of numeric value. Let's call
First word of warning, I believe this is a stackoverflow rather than programmers question,
I have a function (exercise 10.11 in Thompson's The Craft of Functional Programming )
I'm creating a program where the kids must convert a decimal value (I.E. 0.xxxx
I need to find the value of n choose r- the number of ways
look the UML image. I've got a generic class called BinaryProblem, this one receives
I'm trying to write a program that modifies fractions and I need to make
I have 3 very large tables with clustered indexes on composite keys. No updates
I'm running this code to calculate square root of x[i][j] - y[j] but it
I am trying to understand the Simulink (continuous) transfer function. In the documentation for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.