5 min readβ’december 30, 2022
Peter Cao
Milo Chang
Finding the Minimum and Maximum
/** Finds the maximum
*/
public static int maximum(int[] array) {
int maxValue = array[0];
for (int number: array) {
if (number > maxValue) {
//if new max value found, replace current maxValue
maxValue = number;
}
}
return maxValue;
}
/** Finds the minimum
*/
public static int minimum(int[] array) {
int minValue = array[0];
for (int number: array) {
if (number < minValue) {
//if new min value found, replace current minValue
minValue = number;
}
}
return minValue;
}
Finding a Sum
/** Sums up all elements in the array
*/
public static int sum(int[] array) {
int sum = 0;
for (int number: array) {
sum += number; //adds every element to sum
}
return sum;
}
Finding a Mean
/** Finds the mean/average of the array
*/
public static int mean(int[] array) {
// find the sum of the array, can be replaced with sum algorithm from above
int sum = sum(array);
// then divide by number of items
return (double) sum / (array.length);
}
Finding a Mode
/** Finds the mode of an array
**Prerequisite:** The array must have a mode
*/
public static int mode(int[] array) {
int mostCommon = 0;
int mostCommonFrequency = 0;
//traverse through the first n-1 elements of the array
for (int i = 0; i < array.length - 1; i++) {
int currentFrequency = 1;
//traverse through the array starting from the next element
for (int j = i + 1; j < array.length; j++) {
if (array[j] == array[i]) {
// if any element matches current element being checked, add 1 to frequency
currentFrequency++;
}
}
if (currentFrequency > mostCommonFrequency) {
// replaces current mode if new most common element
mostCommon = array[i];
mostCommonFrequency = currentFrequency;
}
}
return mostCommon; // can also be modified to return the frequency
}
Determining If All Values Have a Certain Property
/** Determines whether all values are even
*/
public static boolean isEven(int[] array) {
//Assume all values are positive first
for (int number: array) {
if (number % 2 == 1) {
//If there is one value that is not positive, return false
return false;
}
}
return true; //No odd numbers were found
}
Accessing All Consecutive Pairs/Triplets/Sequences of Length n of Elements
/** Returns all consecutive sequences of length n in the array
*/
public static void returnAllConsecutiveSequences(int[] array, int length) {
for (int i = 0; i <= array.length - length; i++) {
for (int j = 0; j < length; j++) {
//2 loops, one to get the starting number, the other to go through the sequences
System.out.print(array[i+j] + " ");
}
System.out.println();
}
}
Checking if There are Duplicate Elements
/** Checks to see if there are duplicate elements
*/
public static boolean duplicates(int[] array) {
for (int i = 0; i < array.length - 1; i++) { //traverse through the array
for (int j = i + 1; j < array.length; j++) { //traverse through rest of array
if (array[j] == array[i]) {
// if any element matches current element being checked, return true
return true;
}
}
}
return false; // if this point reached, no duplicates found
}
Determining How Many Elements Fit a Criteria
/** Returns how many even numbers there are
*/
public static int evenFrequency(int[] array) {
int numberEven = 0;
for (int number: array) {
if (number % 2 == 0) {
// increments every time an even integer is found
numberEven++;
}
}
return numberEven;
}
Shifting Elements One Index Left
/** Shifts Elements One Index to the Left
*/
public static int[] shiftLeft(int[] array) {
int firstItem = array[0]
for (int i = 0; i < array.length - 1; i++) {
// Does the shifting
array[i] = array[i+1];
}
array[array.length - 1] = firstItem;
return array;
}
Shifting Elements One Index Right
/** Shifts Elements One Index to the Right
*/
public static int[] shiftRight(int[] array) {
int lastItem = array[array.length - 1]
for (int i = array.length - 1; i > 0; i--) {
// Does the shifting
array[i] = array[i-1];
}
array[0] = lastItem;
return array;
}
Reversing an Array
/** Reverses the array
*/
public static int[] reverse(int[] array) {
int[] newArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
// places the items in the new array in opposite order of the original
newArray[i] = array[array.length - i - 1];
}
return newArray;
}
Β© 2023 Fiveable Inc. All rights reserved.