   Mode Function in Java.. help!
  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);}}
  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".   ;  Hmm.. let me try that. I'll post again if I don't get it, so please check back.thanks!   ;  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);}}};  Got it! Just had to add this..if (tempNum != number){countTwo = count;numberTwo = number;}Thanks MikeTacular!   ;  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!   ;  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?   ;  Have you ever tried debugging this? If you`re using NetBeans, the debugger is really great!   ;Quote:Original post by musafir2007One 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.   