GeeksforGeeks » Interview Questions

find the element appearing n times in an array of 2n elements

(17 posts)
  1. raman
    guest
    Posted 1 year ago #

    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

  2. cristigologan
    guest
    Posted 1 year ago #

    median :)

  3. raman
    guest
    Posted 1 year ago #

    It is not median as array is not sorted. And sorting is not a good idea as expected time complexity is O(n).

  4. blunderboy
    Member
    Posted 1 year ago #

    We can apply hashing, as soon as we find an element which gets repeated, we will stop as all other elements are different.
    Please correct me if i am wrong.

  5. Ravi
    guest
    Posted 1 year ago #

    hashing cannot be used because of collision problem. As n can be very large or other elements can be very large.

  6. blunderboy
    Member
    Posted 1 year ago #

    I have thought of an approach to the new problem (2n numbers).

    Just remove the last element from the array and store in temp.
    Now there are two cases.
    Case1. The removed element is the element repeating n times.
    That means (n-1) elements same and n elements different.
    Case2. The removed element is normal element.
    That means n elements same and n-1 different.

    Now, Just traverse the array using voting algorithm.You will get an element but not sure whether it is majority or not.
    And we also have another element stored in temp.

    We are sure that ur desired element is either the temp or the element coming as candidate from the algorithm.

    So traverse the array once again to count their frequency.
    Print the desired element.

    Please correct me.

  7. Tarun
    Member
    Posted 1 year ago #

    Hash can be used, keep increasing count of number index and any index count greater that 1 is repeated number n times. In worst case need to traverse n+1 times. complexity O(n) and space complexity O(n)

  8. expert
    guest
    Posted 1 year ago #

    i think , finding n/2 smallest element will work...
    using quick select we can find it easily...

    plz correct if mistaken...

  9. google
    Member
    Posted 1 year ago #

    no , it's not the n/2 element actually...
    the repeating element will be N th or (N+1)th element which can be found using quick select ...
    so if nth and n+1 th element are same ... we got the answer...

    but if both differ then we can find the maximum element and if it is equal to n+1 th element then it is the answer else nth element is the answer...

  10. Ankit
    guest
    Posted 1 year ago #

    Create a binary tree with given array elements. Each node should have 2 int fields ,it's value and count (and for any node ,all left childs' values should be less and right childs' values should be grater). When duplicates are found increment the count field of the node.

    Now, after creation of binary tree,just traverse the tree and check for which node count value is n.

  11. guest1
    guest
    Posted 12 months ago #

    None of the above solution are utilizing the fact : and the remaining n elements are all different.

  12. ab
    guest
    Posted 12 months ago #

    take a set of ((total num of element)/(no of equal element) + 1), what happens is, of you have to keep n equal element in a set of 2n elements, there must be "atleast" one case where the particular element in question will come twice in consecutive 3 elements. So after making a set of 3 elements, check if any number is present twice in the set. If not, remove the first element and add the next element in the set and continue.

  13. satya
    guest
    Posted 12 months ago #

    modified Moore voting theorem as we used to find majority element..isn't it..???

  14. guest1
    guest
    Posted 12 months ago #

    I am not able to understand why majority voting algorithm would not work. O(n) time solution without space.

  15. ab
    guest
    Posted 12 months ago #

    @guest1: I think majority voting algorithms also will give a nice and small approach
    below is the code

    #include<stdio.h>
    int array[] = {9,3,4,3,5,3,3,7};
    void main()
    {
    	int i = 0;
    	int value[2] = {array[0],1};
    	int size = sizeof(array)/sizeof(int);
    	for(i = 1;i < size;i++)
    	{
    		if(value[0] == array[i])
    		{
    			value[1]++;
    		}
    		else
    		{
    			value[1]--;
    			if(value[1] <= 0)
    			{
    				value[0] = array[i];
    				value[1] = 1;
    			}
    		}
    	}
    	printf("\nThe required element is %d\n",value[0]);
    
    }
  16. rajcools
    Member
    Posted 12 months ago #

    moore's algorithm is helpful when in a set of n elements an element is present n/2+1 times. So in our case it is not full proof....
    read this post[commenst for solution to this particular problem]...
    http://geeksforgeeks.org/?p=503

  17. Tyson
    guest
    Posted 5 months ago #

    Using Voting Algorithm can work ...
    1> It will return the maximum occurrence element ... o(N)
    2> Now traverse again to find the count of that element obtained above ...
    We can find out the count of that element tooo ....

    ### Since it is given that N occurence of one element, therefore their should not be any element whose occurence is equals to N...

    So time Complexity O(n) ...


Reply

You must log in to post.

RSS feed for this topic