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: 57 additions & 12 deletions src/Practice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public class Practice {

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n)
// Space Complexity: O(n)
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)
// Space Complexity: O(1)
public static int sumDiagonal(int[][] matrix) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
Expand All @@ -28,8 +28,8 @@ public static int sumDiagonal(int[][] matrix) {
}


// Time Complexity:
// Space Complexity:
// 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 @@ -49,16 +49,38 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) {
*
* Once you finish, WRITE TESTS FOR IT in PracticeTest.java
*
* Time Complexity:
* Space Complexity:
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* @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;
if (nums.length == 0 || nums == null) {
throw new IllegalArgumentException("Array is null or empty");
} else {
// will hold number at first index and initiate count
int number = nums[0];
int freqCount = 0;
// stores numbers and frequencies in a hashmap
Map<Integer, Integer> numCount = new HashMap<>();
// iterate through and count, update number/count if one with more apperances is found
for (int num: nums) {
int count = numCount.getOrDefault(num, 0) + 1;
numCount.put(num, count);

if (count > freqCount) {
number = num;
freqCount = count;
} else if (count == freqCount && num > number) {
number = num;
}
}
// return most frequent number
return number;
}
}

/**
Expand All @@ -70,15 +92,38 @@ public static int mostCommonTimeEfficient(int[] nums) {
*
* Once you finish, WRITE TESTS FOR IT in PracticeTest.java
*
* Time Complexity:
* Space Complexity:
* Time Complexity: O(n^2)
* 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;
if (nums.length == 0 || nums == null) {
throw new IllegalArgumentException("Array is null or empty");
} else if (nums.length == 1) {
return nums[0];
} else {
int number = 0;
int count = 0;
for (int i : nums) {
int currentNum = i;
int currentCount = 0;
for (int j = 0; j < nums.length; j++) {
if (currentNum == nums[j]) {
currentCount++;
}
}
if (currentCount > count) {
count = currentCount;
number = currentNum;
} else if (currentCount == count && currentNum > number) {
number = currentNum;
}
}
return number;
}
}
}
74 changes: 74 additions & 0 deletions src/PracticeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,78 @@ public class PracticeTest {

// Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls
// Forgot how to do unit tests? Look back at the ramblebot tests or old slides for a refresher!

@Test
void testMostCommonTimeEfficient() {
int[] nums = {4, 4, 2, 5, 1, 7, 3};
int commonNum = Practice.mostCommonTimeEfficient(nums);
assertEquals(4, commonNum);
}

@Test
void testMostCommonSpaceEfficient() {
int[] nums = {3, 3, 4, 5, 1, 3, 3};
int commonNum = Practice.mostCommonSpaceEfficient(nums);
assertEquals(3, commonNum);
}

@Test
void testMostCommonSpaceEfficientOneElement() {
int[] nums = {3};
int commonNum = Practice.mostCommonSpaceEfficient(nums);
assertEquals(3, commonNum);
}

@Test
void testMostCommonTimeEfficientOneElement() {
int[] nums = {3};
int commonNum = Practice.mostCommonTimeEfficient(nums);
assertEquals(3, commonNum);
}

@Test
void testMostCommonSpaceEfficientTie() {
int[] nums = {7, 7, 2, 2, 1, 4, 4, 1};
int commonNum = Practice.mostCommonSpaceEfficient(nums);
assertEquals(7, commonNum);
}

@Test
void testMostCommonTimeEfficientTie() {
int[] nums = {7, 7, 9, 9, 1, 8, 8, 1};
int commonNum = Practice.mostCommonTimeEfficient(nums);
assertEquals(9, commonNum);
}

@Test
void testMostCommonTimeEfficientSameElements() {
int[] nums = {9, 9, 9, 9};
int commonNum = Practice.mostCommonTimeEfficient(nums);
assertEquals(9, commonNum);
}

@Test
void testMostCommonSpaceEfficientSameElements() {
int[] nums = {9, 9, 9, 9};
int commonNum = Practice.mostCommonSpaceEfficient(nums);
assertEquals(9, commonNum);
}

@Test
void testMostCommonTimeEfficientNoElements() {
int[] nums = {};
// Uses a lambda expression to wrap the method call to be tested
assertThrows(IllegalArgumentException.class, () -> {
Practice.mostCommonTimeEfficient(nums);
});
}

@Test
void testMostCommonSpaceEfficientNoElements() {
int[] nums = {};
// Uses a lambda expression to wrap the method call to be tested
assertThrows(IllegalArgumentException.class, () -> {
Practice.mostCommonSpaceEfficient(nums);
});
}
}