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
90 changes: 76 additions & 14 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) | n = array.length
// Space Complexity: O(n) | n = evens.length
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 = 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 @@ -28,8 +28,8 @@ public static int sumDiagonal(int[][] matrix) {
}


// Time Complexity:
// Space Complexity:
// Time Complexity: O(n) | n = Map iteration
// Space Complexity: O(n) | n = frequencies
// 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,8 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) {
return frequencies;
}

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n^2) | n = n argument
// Space Complexity: O(n) | n = n argument
public static List<Integer> evensToSquare(int n) {
List<Integer> evens = new ArrayList<>();
for(int i = 0; i <= n*n; i+=2) {
Expand All @@ -59,16 +59,61 @@ 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) | n = nums.length/iteration
* Space Complexity: O(n) | n = nums.length
*
* @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;

// Empty Handling
if (nums == null || nums.length == 0)
{
return 0;
}

Map<Integer, Integer> numfreq = new HashMap<>();

for (int i = 0; i < nums.length; i++)
{
if (numfreq.containsKey(nums[i]))
{
int keyAdded = numfreq.get(nums[i]) + 1;
numfreq.replace(nums[i], keyAdded);
}
else
{
numfreq.put(nums[i], 1);
}

}

List<Integer> highestNums = new ArrayList<>(numfreq.values());
int numCount = 0;

for (int num : highestNums)
{
if (num > numCount)
{
numCount = num;
}

}

List<Integer> mostFrequentKey = new ArrayList<>();
for (int num : numfreq.keySet())
{
if (numfreq.get(num) == numCount)
{
mostFrequentKey.add(num);
}
}

return mostFrequentKey.get(0);

}

/**
Expand All @@ -80,15 +125,32 @@ 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) | n = nums.length
* 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;
int finalNum = 0;
int finalCount= 0;

for(int i = 0; i < nums.length; i++) {
int currNum = nums[i];
int currCount = 1;
for(int j = i + 1; j < nums.length; j++) {
if(currNum == nums[j]) {
currCount++;
}
}
if(finalCount < currCount) {
finalCount = currCount;
finalNum = nums[i];
}
}

return finalNum;
}
}
78 changes: 78 additions & 0 deletions src/PracticeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,82 @@ 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
void testMostCommonTimeEfficient_oneMax()
{
// Arrange
int[] input = {1, 5, 3, 7, 4, 4};
// Act
int actual = Practice.mostCommonTimeEfficient(input);
// Assert
assertEquals(4, actual);
}

@Test
void testMostCommonTimeEfficient_multiMax()
{
// Arrange
int[] input = {2, 6, 4, 5, 4, 5, 3, 9, 9, 5, 4};
// Act
int actual = Practice.mostCommonTimeEfficient(input);
// Assert
assertEquals(4, actual);
}

@Test
void testMostCommonTimeEfficient_emptyInput()
{
// Arrange
int[] input = {};
// Act
int actual = Practice.mostCommonTimeEfficient(input);
// Assert
assertEquals(0, actual);

}

@Test
void testMostCommonTimeEfficient_singleInput()
{
// Arrange
int[] input = {1};
// Act
int actual = Practice.mostCommonTimeEfficient(input);
// Assert
assertEquals(1, actual);
}


// ******************** TESTS of mostCommonSpaceEfficient ********************
@Test
void testMostCommonSpaceEfficient_basicInput() {
// Arrange
int[] input = {1, 3, 5, 1, 4, 1, 5, 4};
// Act
int actual = Practice.mostCommonSpaceEfficient(input);
// Assert
assertEquals(1, actual);
}

@Test
void testMostCommonSpaceEfficient_twoCommonNums() {
// Arrange
int[] input = {1, 2, 3, 2, 3, 2, 4, 3, 4, 5};
// Act
int actual = Practice.mostCommonSpaceEfficient(input);
// Assert
assertEquals(2, actual);
}

@Test
void testMostCommonSpaceEfficient_sameAmountOfNums() {
// Arrange
int[] input = {1, 3, 2, 7, 4, 5, 6};
// Act
int actual = Practice.mostCommonSpaceEfficient(input);
// Assert
assertEquals(1, actual);
}
}