// counts how many times element is in arr, the data type can be
changed
public static int countOccurrencesA(int[] arr, int element) {
int count = 0;
for (int num : arr) {
if (num == element) {
count++;
}
}
return count;
}
// sorts an array, the data type can be changed(realistically only to
numeric values)
public static void sortArray(int[] arr) {
if (arr.length == 0) {
} else {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
// checks a chosen value exists in arr, data type can be changed
public static boolean containsElementA(int[] arr, int element) {
for (int num : arr) {
if (num == element) {
return true;
}
}
return false;
}
// creates an array of integers with the indexes at which a chosen value
appears in the array, data type can be changed
public static int[] OcValA(int[] arr, int val) {
int count = 0;
for (int num : arr) {
if (num == val) {
count++;
}
}
int[] indices = new int[count];
int idx = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == val) indices[idx++] = i;
}
return indices;
}
// insert a specific value to a chosen index in the array and creates an
array with the inserted value, x = {1, 2, 3}; Int[] y = insertArray(x, 0,
100}; y = {100, 1, 2, 3} // data type can be changed
public static int[] insertArray(int[] array, int index, int value) {
int[] tempArray = copyA(array, array.length + 1);
int currentIndex = 0;
boolean inserted = false;
for (int i = 0; i < tempArray.length; i++) {
if (currentIndex == index) {
tempArray[i] = value;
inserted = true;
} else if (currentIndex > index) {
tempArray[i] = array[i - 1];
} else {
tempArray[i] = array[i];
}
currentIndex++;
}
if (!inserted)tempArray[array.length] = value;
return tempArray;
}
// checks if arr2 is a sub array of array 1, arr2 = {1, 2}; arr1 = {0, 1, 2,
3, 4}; subArray(arr1, arr2) = true, but if subArray(arr2, arr1) than =
false.
public static boolean subArray(int[] arr1, int[] arr2) {
for (int i = 0; i <= arr1.length - arr2.length; i++) {
boolean isSubArray = true;
for (int j = 0; j < arr2.length; j++) {
if (arr1[i + j] != arr2[j]) {
isSubArray = false;
break;
}
}
if (isSubArray) {
return true;
}
}
return false;
}
// counts how many times element is in str
public static int countOccurrencesS(String str, String element) {
return (OcValS(str, element).length() + 1)/2;
}
//creates String with a change to a specific index of a string with a
specific value, x = “ABCDEFG”,
y = setS(x, 0, “DE”) = DEBCEFG
public static String setS(String str, int index, String value) {
if (index >= 0 && index < str.length())
return str.substring(0, index) + value + str.substring(index + 1);
return str;
}
// checks if a string is a substring of another string
public static boolean subString(String str, String subStr) {
for (int i = 0; i <= str.length() - subStr.length(); i++) {
boolean found = true;
for (int j = 0; j < subStr.length(); j++) {
if (str.charAt(i + j) != subStr.charAt(j)) {
found = false;
break;
}
}
if (found) {
return true;
}
}
return false;
}
// checks if a specific value is in a String
public static boolean containsElementS(String str, String element) {
for (int i = 0; i <= str.length() - element.length(); i++) {
if (str.substring(i, i + element.length()).equals(element)) {
return true;
}
}
return false;
}
// returns a String with the indexes of where val is in String,
x = “ABCDBCDB”; val = “B”; OcVals(x, val) = 1 4 7, it returns the
indexes with a space which separates them
public static String OcValS(String str, String val) {
String indices = "";
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals(val))
indices += i + " ";
}
return indices;
}
// creates a String without a value at a specific index of a String
public static String deleteS(String str, int index) {
if (index >= 0 && index < str.length()) {
return str.substring(0, index) + str.substring(index + 1);
}
return str;
}
// creates a string with an inserted value at a specific index,
public static String insertS(String str, int index, String value) {
if (index >= 0 && index <= str.length())
return str.substring(0, index) + value + str.substring(index);
return str;
}
//checks if a number is a prime number
public static boolean isPrime(int num) {
if (num <= 1)return false;
if (num == 2)return true;
if (num % 2 == 0)return false;
int sqrt = (int) Math.sqrt(num);
for (int i = 3; i <= sqrt; i += 2)return false;
}
return true;
}
//checks if a String can be divided into two different parts
public static boolean isDouble(String str){
if(str.length() % 2 == 1 || str.length() == 0) return false;
return
str.substring(0, str.length()/2).equals(str.substring(str.length()/2));
}
// removes every integer which is less or equal to 0, x = {1, 2, 0, -1}
int[] y = positiveNoneZeroIntegersArray(x) = {1, 2}
public static int[] positiveNoneZeroIntegerArray(int[] x){
if(x.length == 0) return x;
int count = 0;
for(int i = 0; i<x.length; i++){
if(x[i] > 0) // doesn't include 0;
count++;
}
int j = 0;
int[] y = new int[count];
for(int i = 0; i<x.length; i++){
if(x[i] > 0){ // doesn't include 0;
y[j] = x[i];
j++;}
}
return y;
}
// creates a copy of an array, int[] x = {1, 2, 3}; int[] y = copyA(x); =
{1, 2, 3}
public static int[] copyA(int[] original, int newLength) {
// int can be changed to any other data type.
int[] copy = new int[newLength];
for (int i = 0; i < Math.min(original.length, newLength); i++) {
copy[i] = original[i];
}
return copy;
}
// checks if a String is a palindrome a word, phrase, or sequence that
reads the same backward as forward
public static boolean isPali(String str) {
if (str == null || str.isEmpty()) return false;
if (str.length() % 2 != 0) {
int middleIndex = str.length() / 2;
String result = str.substring(0, middleIndex) +
str.substring(middleIndex + 1);
for (int i = 0; i < str.length() / 2; i++) {
if (result.charAt(i) != result.charAt(result.length() - 1 - i)) {
return false;
}
}
return true;
}
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
}
// moves all the elements between start and end including start and
end to the index of newStart and moves the element in newStart to
after the element of end,
//need to write Integer x instead of int[] x here
// here tis Integer[] x = new Integer[6]
Integer[] x = {1, 2, 3, 4, 5, 6}; moveTo(x, 1, 3, 0) // can’t move to
anywhere between
1=<newStart<3
Here we chose the indexes 1-3 which is {2, 3, 4} to move to index 0
which is {1}
So after using the method x will be: x = {2, 3, 4, 1, 5, 6}
Let x = {1, 2, 3, 4, 5, 6}
To move the chosen indexes to the last index newStart needs to be
equal to:
((x.length-1)-((end)- (start))) so in our case its ((6-1)-(3-1)) = 3 so to
move {2, 3, 4} to the last index the operation will look like this:
moveTo(x, 1, 3, 3) it’ll result in x = {1, 5, 6, 2, 3, 4}
public static <T> void moveTo(T[] arr, int start, int end, int newStart) {
if (newStart >= start && newStart <= end) return;
int length = end - start + 1;
T[] temp = (T[]) new Object[length];
for (int i = 0; i < length; i++) temp[i] = arr[start + i];
for (int i = end + 1; i < arr.length; i++) arr[i - length] = arr[i];
int j = arr.length - 1;
for (int i = j; i >= newStart + length; i--) arr[i] = arr[i - length];
for (int i = newStart; i < newStart + length; i++) arr[i] = temp[i -
newStart];
}
//takes the k variables from the end and reverses them and inserts
them to the start.
For example rotateArray(x, 2) for x = {1, 2, 3, 4, 5} results in x = {4,
5, 1, 2, 3}
public static <T> void rotateArray(T[] arr, int k) {
int n = arr.length;
k = k % n;
reverseArray(arr, 0, n - 1);
reverseArray(arr, 0, k - 1);
reverseArray(arr, k, n - 1);
}
// reverses an array
public static <T> void reverseArray(T[] arr, int start, int end) {
while (start < end) {
T temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
// finds the max value of array the other one finds the min value
public static int findMax(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
return max;
}
public static int findMin(int[] arr) {
int min = arr[0];
for (int num : arr) {
if (num < min) min = num;
}
return min;
}
Magnitude of running time, order of DE via power of n.
1. countOccurrencesA = O(n)
2. sortArray = O(n^2)
3. containsElementA = O(n)
4. OcValA = O(n)
5. insertArray = O(n)
6. subarray = O(n^2)
7. countOccurrencesS = O(n^2)
8. setS = O(n)
9. subString = O(n^2)
10. containsElementS = O(n^2)
11. OcValS = O(n)
12. deleteS = O(n)
13. insertS = O(n)
14. isPrime = O(n)
15. isDouble = O(n)
16. positiveNoneZeroIntegerArray = O(n)
17. copyA = O(n)
18. isPali = O(n)
19. moveTo = O(n)
20. routateArray = O(n)
21. reverseArray = O(n)
22. findMax = O(n)
23. findMin = O(n)
how to use Math.random:
int min;
int max;
int randomNum = (int)(Math.random() * (max - min + 1)) + min;
Output:
A random number between min (inclusive) and max (inclusive)