Find the Mode - Exercise

Contact Us or call 1-877-932-8228
Find the Mode - Exercise

Find the Mode

Duration: 20 to 30 minutes.

In this exercise, you will use Array methods to find the mode - element(s) occurring most frequently - of an array.

  1. Open AdvancedArrays/Exercises/mode.html for editing.
  2. Assume that the array nums always contains integer elements greater than or equal to 0.
  3. Write method Mode which returns an array of the mode(s) from its integer parameter arr; for example:
    • Mode([1,1,2,1]) would return [1]
    • Mode([0,17,17,9,2,9]) would return [9,17]
    • Mode([1,2]) would return [1,2]
  4. One strategy for finding the mode is:
    • Find the maximum value (max) in the array (hint: use reduce);
    • Create a new array with with length max + 1 to record the frequencies of occurrence of each element;
    • Set the value of each element in the frequency array to 0;
    • Iterate over the original array, incrementing the value of the frequency table whose index is the current value from the original array;
    • Find the maximum value from the frequency table;
    • Return an array containing the indices of all elements in the frequency table whose value is the maximum value.
  5. Test your solution in a browser.

Challenge

Design a Mode method that will work for an array where some elements may be negative.

Solution:

AdvancedArrays/Solutions/mode.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Mode</title>
</head>
<body>
	<h1>Mode</h1>
	<script>
		var nums = [92, 2, 33, 92, 33, 7, 20, 20, 20, 33, 57, 92, 41, 7];
		document.write(nums);

		function mode(arr) {
			var max = arr.reduce( (n, m) => n > m ? n : m );
			var frequencies = new Array(max + 1);
			var modes = [];

			frequencies.fill(0);

			arr.forEach(function(value) {
				frequencies[value] = frequencies[value] + 1;
			});

			var maxFrequency = frequencies.reduce( (n, m) => n > m ? n : m );

			var maxOccurringElement = frequencies.indexOf(maxFrequency);
			while (maxOccurringElement != -1) {
				modes.push(maxOccurringElement);
				maxOccurringElement = frequencies.indexOf(maxFrequency, maxOccurringElement + 1);
			}
			return modes;
		}

		document.write('<hr>');
		var mode = mode(nums);
		document.write(mode);
	</script>
</body>
</html>

Code Explanation

The reduce method offers an easy way to find the maximum value in arr: compare each adjacent element in the array and return the greater value; we set local variable max to the greatest value in arr.

We create local array frequencies with length max + 1 and, using the fill method, set all of its values to 0.

We iterate over arr using forEach and increment the element of frequencies whose index correponds to the current element from arr. When forEach finishes, the value of any element in the frequencies array is the frequency of occurrence of its index as an element in array arr.

Again using reduce, we find the maximum value in frequencies and set maxFrequency; this is the number of times that the most-frequently-occurring element(s) were found in arr.

We use indexOf to find all of the indices from the frequencies array whose value is maxFrequency, adding each to the array modes and return modes.

Challenge Solution:

AdvancedArrays/Solutions/mode-challenge.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Mode</title>
</head>
<body>
	<h1>Mode</h1>
	<script>
		var nums = [-1, -1, -1, 10, 10, 100, 99, 99, 99];
		document.write(nums);

		function mode(arr) {
			var max = arr.reduce( (n, m) => n > m ? n : m );
			var min = arr.reduce( (n, m) => n < m ? n : m );
			            var conversionValue = min < 0 ? Math.abs(min) : 0;
			            var frequencies = new Array(max + conversionValue + 1);
			var modes = [];

			frequencies.fill(0);

			arr.forEach(function(value) {
				frequencies[value + conversionValue] = frequencies[value + conversionValue] + 1;
			});

			var maxFrequency = frequencies.reduce( (n, m) => n > m ? n : m );

			var maxOccurringElement = frequencies.indexOf(maxFrequency);
			while (maxOccurringElement != -1) {
				modes.push(maxOccurringElement - conversionValue);
				maxOccurringElement = frequencies.indexOf(maxFrequency, maxOccurringElement + 1);
			}
			return modes;
		}

		document.write('<hr>');
		var mode = mode(nums);
		document.write(mode);
	</script>
</body>
</html>

Code Explanation

We adopt the same basic strategy in finding the mode(s) for arrays with negative values, but here we find the minimum value of arr and, if less than 0, set a local variable conversionValue equal to the absolute value of the minimum value.

We use conversionValue to "shift" the frequencies array, recording the frequency of the most-negative (least) value from arr in the 0th element of frequencies.

We record frequencies as before. When building the array (modes) to return as our answer, we "unshift" the indices with modes.push(maxOccurringElement - conversionValue).

Next