Jump to content
xisto Community
varalu

Data Structure -- Arrays -- Odd Number Of Elements

Recommended Posts

Given an array of elements with many numbers occurring even number of times and two numbers occurring odd number of times. Find out the two numbers that occur odd number of times.

 

example:

 

Elements in array -- 14433446

 

The expected result is 1 and 6

 

 

One solution

1. Find max of the array

2. Hash Function : element/max value

3. Repeat to all elements...

4. Find frequency... yo will get the 2 elements with odd frequency.

 

but this is not the optimal....

Do find more solutions to this and post it. Look for time and space complexity.

 

 

Another Solution

one more solution

1. Scan through the array

2. Insert the element into a BST if its already not there. If its already avl, delete it from BST.

3. Repeat for all the elements

4. Now, BST will have only the elements with odd occurrence.

 

Time complexity : O(n*log n)

Courtesy: Varun. (friend)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.