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

  • SEARCH
  • Home
  • 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 6150801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:40:14+00:00 2026-05-23T19:40:14+00:00

Given an array of 2n elements of which n elements are same and the

  • 0

Given an array of 2n elements of which n elements are same and the remaining n elements are all different. Write a C program to find out the value which is present n times in the array

I thought in the following way-
compare a[i] and a[i+1] and
compare a[i] and a[i+2]
and return element

this will run in O(n) time..can anyone give better solution?

I was going through some solutions where it is saying like this-

  1. Declare two variables a) count variable to keep track of count of majority element.
  2. Majority element.
  3. Do a for loop and repeat following steps 4-6 until end of array is reached.
  4. if current array element equals majority element, increment count
  5. else, if count is 0, update majority element with current array element and increment count.
  6. else if count is not 0 then decrement count.
  7. Do another for loop and count the number of occurrences of majority element in the array, if it’s half the array size then we have found the majority element, else there’s no majority element.
  • 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-23T19:40:16+00:00Added an answer on May 23, 2026 at 7:40 pm

    You can use the majority element algorithm as a basis for an O(n) solution with O(1) space.

    You need space for one stored element. Pick the first element and store it, if the next element is the same as the stored one, you finished. If not, restart the algorithm from the next step. If you don’t find the element after the end of this, it means that the element are ordered in pairs like (a,b),(a,c),(a,d) or (b,a),(a,c),(a,d),(e,a).. Compare the first four elements and you have found the duplicate.

    Since elements can be in arbitrary order, whichever first n/2 elements you check it’s possible that they are all different. So there is no solution which is bettern than O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array. How can we find sum of elements in index interval (i,
Given an array of items, each of which has a value and cost ,
I have a string. I need to replace all instances of a given array
Given an array of characters which forms a sentence of words, give an efficient
Given an array of N elements representing the permutation atoms, is there an algorithm
I'm planning to write a program in Ruby to analyse some data which has
Problem: Given a matrix in which each row and each column is sorted, write
Given a 1*N matrix or an array, how do I find the first 4
I have an array (of 9 elements, say) which I must treat as a
Given an array of n Objects, let's say it is an array of strings

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.