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
102 changes: 87 additions & 15 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Practice {

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

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n)
// Space Complexity: O(1)
// n is the length of the matrix
public static int sumDiagonal(int[][] matrix) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
Expand All @@ -28,8 +31,9 @@ public static int sumDiagonal(int[][] matrix) {
}


// Time Complexity:
// Space Complexity:
// Time Complexity: O(n)
// Space Complexity: O(n))
// n is the length of array
// 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 +44,9 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) {
return frequencies;
}

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n^2)
// Space Complexity: O(n^2)
// n is the value of int 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,16 +64,49 @@ 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)
* // n is the length of nums
*
* @param nums An array of integers
* @return the integer that shows up most commonly
*/
public static int mostCommonTimeEfficient(int[] nums) {
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)
{
return -1;
}

Map<Integer, Integer> intMap = new HashMap<>();
for(Integer num : nums)
{
if(intMap.containsKey(num))
{
intMap.put(num, intMap.get(num) + 1 );
}
else{
intMap.put(num, 1 );
}
}

int maxValue = 0;
int keyWithMaxValue = 0;


for (int key : intMap.keySet())
{
if (intMap.get(key) > maxValue)
{
maxValue = intMap.get(key);
keyWithMaxValue = key;
}
}
return keyWithMaxValue;

}

/**
Expand All @@ -86,9 +124,43 @@ public static int mostCommonTimeEfficient(int[] nums) {
* @param nums An array of integers
* @return the integer that shows up most commonly
*/
public static int mostCommonSpaceEfficient(int[] nums) {

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)
{
return -1;
}

Arrays.sort(nums); //sorted the list from smallest to largest

int count = 1;
int maxCount = 1;
int current = nums[0];
int mostCommon = nums[0];

for(int i = 1; i < nums.length; i++)
{
if(nums[i] == current)
{
count ++;
}

else
{
current = nums[i];
count = 1;
}

if (count > maxCount)
{
maxCount = count;
mostCommon = current;

}
}
return mostCommon;
}
}
120 changes: 120 additions & 0 deletions src/PracticeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,124 @@ 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 method //

@Test
void mostCommonTimeEfficient_SingleElement()
{
// arrange
int[] testArray = {100};
// act
int actual = Practice.mostCommonTimeEfficient(testArray);
//assert
assertEquals(100, actual);
}

@Test
void mostCommonTimeEfficient_OneMostCommonValue()
{
// arrange
int[] testArray = {1, 2, 3, 1};
// act
int actual = Practice.mostCommonTimeEfficient(testArray);
//assert
assertEquals(1, actual);
}

@Test
void mostCommonTimeEfficient_MultipleMostCommonValues()
{
// arrange
int[] testArray = {1, 2, 3, 1, 2, 2, 3, 3, 1};
// act
int actual = Practice.mostCommonTimeEfficient(testArray);
//assert
assertEquals(1, actual);
}

@Test
void mostCommonTimeEfficient_EmptyArray()
{
// arrange
int[] testArray = {};
// act
int actual = Practice.mostCommonTimeEfficient(testArray);
//assert
assertEquals(-1, actual);
}

@Test
void mostCommonTimeEfficient_NegativeElements()
{
// arrange
int[] testArray = {-1, 1, -2, 3, -1, 2, -3};
// act
int actual = Practice.mostCommonTimeEfficient(testArray);
//assert
assertEquals(-1, actual);
}





//Tests for most common space efficient
@Test
void mostCommonSpaceEfficient_SingleElement()
{
// arrange
int[] testArray = {100};
// act
int actual = Practice.mostCommonSpaceEfficient(testArray);
//assert
assertEquals(100, actual);
}

@Test
void mostCommonSpaceEfficient_OneMostCommonValue()
{
// arrange
int[] testArray = {1, 2, 3, 1};
// act
int actual = Practice.mostCommonSpaceEfficient(testArray);
//assert
assertEquals(1, actual);
}

@Test
void mostCommonSpaceEfficient_MultipleMostCommonValues()
{
// arrange
int[] testArray = {1, 2, 3, 1, 2, 2, 3, 3, 1};
// act
int actual = Practice.mostCommonSpaceEfficient(testArray);
//assert
assertEquals(1, actual);
}

@Test
void mostCommonSpaceEfficient_EmptyArray()
{
// arrange
int[] testArray = {};
// act
int actual = Practice.mostCommonSpaceEfficient(testArray);
//assert
assertEquals(-1, actual);
}

@Test
void mostCommonSpaceEfficient_NegativeElements()
{
// arrange
int[] testArray = {-1, 1, -2, 3, -1, 2, -3};
// act
int actual = Practice.mostCommonSpaceEfficient(testArray);
//assert
assertEquals(-1, actual);
}



}