Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Practice {

// Time Complexity:
// Space Complexity:
// Time Complexity: o(n) n is array size
// Space Complexity: o(n) n is new list
public static List<Integer> findEvens(int[] array) {
List<Integer> evens = new ArrayList<>();
for (int num : array) {
Expand All @@ -17,8 +17,8 @@ public static List<Integer> findEvens(int[] array) {
return evens;
}

// Time Complexity:
// Space Complexity:
// Time Complexity: o(n) n is matrix length
// Space Complexity: o(1)
public static int sumDiagonal(int[][] matrix) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
Expand All @@ -27,9 +27,9 @@ public static int sumDiagonal(int[][] matrix) {
return sum;
}


// Time Complexity:
// Space Complexity:
//n = T
// Time Complexity: O(n)
// Space Complexity: O(n)
// Does the 'T' look confusing? Consider refreshing on generic methods
// We'll revisit generics as a class later
public static <T> Map<T, Integer> countFrequencies(T[] array) {
Expand All @@ -40,8 +40,9 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) {
return frequencies;
}

// Time Complexity:
// Space Complexity:
//n = n*n
// Time Complexity: O(n)
// Space Complexity: O(n)
public static List<Integer> evensToSquare(int n) {
List<Integer> evens = new ArrayList<>();
for(int i = 0; i <= n*n; i+=2) {
Expand All @@ -59,17 +60,32 @@ public static List<Integer> evensToSquare(int n) {
*
* Once you finish, WRITE TESTS FOR IT in PracticeTest.java
*
* Time Complexity:
* Space Complexity:
* Time Complexity: O(n)
* Space Complexity: O(n) (potentially n number of entries)
*
* @param nums An array of integers
* @return the integer that shows up most commonly
*/
public static int mostCommonTimeEfficient(int[] nums) {
// TODO: Complete this method with an implementation that runs
// in O(n) time. n = nums.size()
return -1;
}

Map<Integer, Integer> mapOfCommonNums = new HashMap<>();
int maxCount = 0;
int mostCommon = nums[0];

// Loop through the array and count how many times a number is read through
for (int commonNumber : nums) {
mapOfCommonNums.put(commonNumber, mapOfCommonNums.getOrDefault(commonNumber,0)+ 1 );
// checks if the common number is greater than the maxCount and then sets it to mostCommon
if (mapOfCommonNums.get(commonNumber) > maxCount) {
maxCount = mapOfCommonNums.get(commonNumber);
mostCommon = commonNumber;
}//end if
}//end for

return mostCommon;
}//end mostCommonTimeEfficient

/**
* Returns the integer that shows up most frequently in an array.
Expand All @@ -80,15 +96,34 @@ public static int mostCommonTimeEfficient(int[] nums) {
*
* Once you finish, WRITE TESTS FOR IT in PracticeTest.java
*
* Time Complexity:
* Space Complexity:
* Time Complexity: o(n)
* Space Complexity: o(1)
*
* @param nums An array of integers
* @return the integer that shows up most commonly
*/
public static int mostCommonSpaceEfficient(int[] nums) {
// TODO: Complete this method with an implementation that runs
// in O(1) space.
return -1;

//empty array
if(nums.length == 0)
return -1;

int count = 0;
int possibleNum = 0;

//finding the num that shows up most (tiebreaker built in)
for(int i = nums.length-1; i >= 0; i--)
{
if(count == 0) //starting number and finding recurring num
possibleNum = nums[i];
if(nums[i] == possibleNum) //checking if num[i] value has already appeared, ++
count ++;
if(nums[i] != possibleNum) //if it hasn't appeared then --
count--;
}

return possibleNum;
}
}
86 changes: 85 additions & 1 deletion src/PracticeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,88 @@ public class PracticeTest {
// TODO: Implement tests for Practice.mostCommonTimeEfficient and Practice.mostCommonSpaceEfficient

// Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls
}

//tests for mostCommonTimeEfficient

@Test
public void timeEfficientMostCommonNumWorking() {
// Arrange
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7};

// Act
int result = Practice.mostCommonTimeEfficient(numbers);

// Assert
assertEquals(7, result); //expected res: 7, most common number
}//end working

@Test
public void timeEfficientMostCommonNumTie() {
// Arrange
int[] nums = {1, 1, 2, 2, 3, 3};

// Act
int result = Practice.mostCommonTimeEfficient(nums);

// Assert
assertEquals(1, result); //expected res: 1, the first most common number is never overwritten since its a tie
}//end tie

@Test
public void timeEfficientMostCommonNumOutOfOrder() {
// Arrange
int[] numerals = {1, 2, 3, 2, 3, 2, 3, 7, 3};

// Act
int result = Practice.mostCommonTimeEfficient(numerals);

// Assert
assertEquals(3, result); //expected res: 3, the most common number but out of order
}// end outOfOrder


//tests for mostCommonSpaceEfficient

@Test
public void spaceEfficientMostCommonNumWorking() {
// Arrange
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7};

// Act
int result = Practice.mostCommonSpaceEfficient(numbers);

// Assert
assertEquals(7, result); //expected res: 7, most common number
}//end working

@Test
public void spaceEfficientMostCommonNumTie() {
// Arrange
int[] nums = {1, 1, 2, 2, 3, 3};

// Act
int result = Practice.mostCommonSpaceEfficient(nums);

// Assert
assertEquals(1, result); //expected res: 1, the first most common number is never overwritten since its a tie
}//end tie

@Test
public void spaceEfficientMostCommonNumOutOfOrder() {
// Arrange
int[] numerals = {1, 2, 3, 2, 3, 2, 3, 7, 3};

// Act
int result = Practice.mostCommonSpaceEfficient(numerals);

// Assert
assertEquals(3, result); //expected res: 3, the most common number but out of order
}// end outOfOrder







}//end practiceTest