Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

Mode Function in Java.. help!

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, So I am suppose to write a function that tells us the number that appears most times in an array. I wrote the following code and it works fine if there is only one mode, but if there are 2 numbers that show up same amount of times my code can't tell. So can someone help me add this feature in which if there are 2 or more modes, it just prints.. "No Majority"? thanks!
public class Majority
{
	public static void main (String[] args)
	{
		int[] numbers = {5, 6, 5, 6, 8, 8, 5, 8, 4, 6, 6, 2};
		majority (numbers);
	}

	static void majority(int[] T)
	{
		int number=0;
		int count = 0;
		for (int i = 0; i < T.length; i++)
		{
			int tempCount = 0;
			int tempNum = T;
			for (int a = 0; a < T.length; a++)
			{
				if (tempNum == T[a])
				{
					tempCount++;
				}
			}
			if (tempCount > count)
			{
				count = tempCount;
				number = tempNum;
			}
		}

		System.out.println ("The Number occurring most times is: " + number);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
I won't tell you straight up how to do it, but I'll give you an idea of one possible solution. You're already able to find which number appears the most (assuming just one). Can you modify your code to track two modes instead of just one? The first mode would be the one that appears the most, and the second mode would be the one that appears the second most. If the first mode appears more than the second one, you know that it is the true mode. However, if the two modes you tracked appear the same number of times, you know there is "No Majority".

Share this post


Link to post
Share on other sites
Why isn't this working?!?


public class Majority
{
public static void main (String[] args)
{
int[] numbers = {5, 6, 5, 6, 8, 8, 5, 8, 4, 6, 6, 8};
majority (numbers);
}

static void majority(int[] T)
{
int number=0;
int numberTwo = 0;
int count = 0;
int countTwo = 0;

for (int i = 0; i < T.length; i++)
{
int tempCount = 0;
int tempNum = T;
for (int a = 0; a < T.length; a++)
{
if (tempNum == T[a])
{
tempCount++;
}
}
if (tempCount >= count)
{
countTwo = count;
numberTwo = number;
count = tempCount;
number = tempNum;
}
}

if (count == countTwo)
{
System.out.println ("No Majority");
}
else
{
System.out.println ("The Number occurring most times is: " + number);
}
}
}


Share this post


Link to post
Share on other sites
When you loop through the array, what happens when you come across a number you've already calculated? You recalculate it. For example, you calculate 5, then 6, and then you calculate 5 again. When 5 is recalculated, what happens to the values of number, count, numberTwo, and countTwo?

[edit]

ninja'd++;

Good to see you got it!

Share this post


Link to post
Share on other sites
One more question.. this is for my data structures class so we are suppose to find the most efficient algorithm with keeping time and space complexity in mind.
Do you think this could've been done better.. less variables/assignments?

Share this post


Link to post
Share on other sites
Quote:
Original post by musafir2007
One more question.. this is for my data structures class so we are suppose to find the most efficient algorithm with keeping time and space complexity in mind.


If space is not a factor, then O(2n) is possible. This could also be O(n), depends on exact interpretation.

Otherwise O(n+nlogn) is trivial without any extra storage. Your current version is O(n^2).

Hint is "sorting". And since this says everything, make sure to find where each of the above cases comes from.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!