@@ -18,6 +18,7 @@ public static void main(String[] args) {
1818 /* Test array of integers */
1919 assert (mode (new int []{})) == null ;
2020 assert Arrays .equals (mode (new int []{5 }), new int []{5 });
21+ assert Arrays .equals (mode (new int []{1 , 2 , 3 , 4 , 5 }), new int []{1 , 2 , 3 , 4 , 5 });
2122 assert Arrays .equals (mode (new int []{7 , 9 , 9 , 4 , 5 , 6 , 7 , 7 , 8 }), new int []{7 });
2223 assert Arrays .equals (mode (new int []{7 , 9 , 9 , 4 , 5 , 6 , 7 , 7 , 9 }), new int []{7 , 9 });
2324
@@ -26,17 +27,16 @@ public static void main(String[] args) {
2627 /*
2728 * Find the mode of an array of integers
2829 *
29- * @param int[] array of integers
30- * @return int[] mode of the array
30+ * @param numbers array of integers
31+ * @return mode of the array
3132 */
3233 public static int [] mode (int [] numbers ) {
3334
3435 if (numbers .length == 0 ) return null ;
3536
36- HashMap <Integer , Integer > count = new HashMap <Integer , Integer >();
37+ HashMap <Integer , Integer > count = new HashMap <>();
3738
3839 for (int num : numbers ) {
39-
4040 if (count .containsKey (num )) {
4141
4242 count .put (num , count .get (num ) + 1 );
@@ -50,20 +50,14 @@ public static int[] mode(int[] numbers) {
5050 }
5151
5252 int max = Collections .max (count .values ());
53- ArrayList <Integer > modes = new ArrayList <Integer >();
53+ ArrayList <Integer > modes = new ArrayList <>();
5454
5555 for (int num : count .keySet ()) {
56-
5756 if (count .get (num ) == max ) {
58-
5957 modes .add (num );
60-
6158 }
62-
6359 }
64-
6560 return modes .stream ().mapToInt (n -> n ).toArray ();
66-
6761 }
6862
6963}
0 commit comments