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
31 changes: 26 additions & 5 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class CollectionBasics {
public static void main(String[] args) {
Expand All @@ -27,9 +28,11 @@ public static void main(String[] args) {
Return the sum of all numbers in the collection
*/
public static int sum(Collection<Integer> numbers) {

int total = 0;

for (int num : numbers) {
total += num;
}
// TODO:
// Loop through the collection
// Add each number to total
Expand All @@ -43,9 +46,13 @@ public static int sum(Collection<Integer> numbers) {
Count how many numbers are even
*/
public static int countEven(Collection<Integer> numbers) {

int count = 0;

for (int num : numbers) {
if (num % 2 == 0) {
count++;
}
}
// TODO:
// Loop through the collection
// If the number is even, increase count
Expand All @@ -59,9 +66,13 @@ public static int countEven(Collection<Integer> numbers) {
Find the largest number in the collection
*/
public static int findMax(Collection<Integer> numbers) {

int max = Integer.MIN_VALUE;

for (int num : numbers) {
if (num > max) {
max = num;
}
}
// TODO:
// Loop through numbers
// Update max if current number is larger
Expand Down Expand Up @@ -90,9 +101,14 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
Count how many times a target value appears
*/
public static int countOccurrences(Collection<Integer> numbers, int target) {

int count = 0;

for (int num : numbers) {
if (num == target) {
count++;
}
}

// TODO:
// Loop through numbers
// If number equals target, increase count
Expand All @@ -107,9 +123,14 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
that only contains numbers greater than 20
*/
public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> numbers) {

Collection<Integer> result = new ArrayList<>();

for (int num : numbers) {
if (num > 20) {
result.add(num);
}
}

// TODO:
// Loop through numbers
// Add numbers greater than 20 to result
Expand Down
191 changes: 173 additions & 18 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package CollectionsHackerrank;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.*;


public class CollectionsHackerrankProblems {
public class CollectionsHackerrankPractice {


public static void main(String[] args) {

Expand All @@ -21,11 +20,17 @@ public static void main(String[] args) {
Input: [1,2,2,3,4,4,5]
Output: [1,2,3,4,5]
*/
public static List<Integer> removeDuplicates(List<Integer> numbers) {



public static List<Integer> removeDuplicates(List<Integer> numbers) {
Set<Integer> set = new LinkedHashSet<>(numbers);//Have to convert List to Set
//Set removes duplicates *LinkedHashSet Keeps the value in order
return new ArrayList<>(set);//Covert the Set back to List
// because the method calls for a List *Set is just for removing Duplicates
// TODO: Implement this method

return null;

}

/*
Expand All @@ -37,10 +42,20 @@ public static List<Integer> removeDuplicates(List<Integer> numbers) {
Output: {1=1, 2=2, 3=3}
*/
public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {

//takes a list of numbers
//returns a map of counts
Map<Integer, Integer> map = new HashMap<>();//This creates an empty HashMap called map
for (int num : numbers) { //for each loop to run through the list
map.put(num, map.getOrDefault(num, 0) + 1);
//This line basically updates the counter for the number
//it Gets the value for this key
// and If the key does not exist, return 0
}

return map;//returns the final results
// TODO: Implement this method

return null;

}

/*
Expand All @@ -52,10 +67,21 @@ public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
Output: 5
*/
public static Integer firstUnique(List<Integer> numbers) {
//THe method header
Map<Integer, Integer> map = countFrequency(numbers);//calling

// TODO: Implement this method
for (int num : numbers) {
if (map.get(num) == 1) {
//get the count for this number
return num;
//returning the number that appears once
}
}

return null;
// TODO: Implement this method


}

/*
Expand All @@ -69,10 +95,21 @@ public static Integer firstUnique(List<Integer> numbers) {
Output: true
*/
public static boolean twoSum(List<Integer> numbers, int target) {
Set<Integer> seen = new HashSet<>();
//This set stores numbers we have already seen.

// TODO: Implement this method
for (int num : numbers) {
if (seen.contains(target - num)) {
//if we already saw the number that would complete the target.
return true;
}
seen.add(num);//adds the number into the seen object that was created
}

return false;
// TODO: Implement this method


}

/*
Expand All @@ -84,10 +121,11 @@ public static boolean twoSum(List<Integer> numbers, int target) {
Output: 3
*/
public static int countUniqueWords(List<String> words) {

Set<String> set = new HashSet<>(words);
return set.size();
// TODO: Implement this method

return 0;

}

/*
Expand All @@ -100,9 +138,27 @@ public static int countUniqueWords(List<String> words) {
*/
public static Queue<Integer> reverseQueue(Queue<Integer> queue) {

// Create a stack to help reverse the order
// Stacks are LIFO (Last In First Out)
Stack<Integer> stack = new Stack<>();

// Move all elements from the queue into the stack
// Queue removes from the front using poll()
while (!queue.isEmpty()) {
stack.push(queue.poll()); // remove from queue and push into stack
}

// Now move everything back from the stack into the queue
// Since stacks remove the last element added, the order becomes reversed
while (!stack.isEmpty()) {
queue.offer(stack.pop()); // pop from stack and add back to queue
}

// Return the reversed queue
return queue;
// TODO: Implement this method

return null;

}

/*
Expand All @@ -118,9 +174,37 @@ public static Queue<Integer> reverseQueue(Queue<Integer> queue) {
*/
public static boolean isBalanced(String expression) {

// Create a stack to keep track of opening parentheses
Stack<Character> stack = new Stack<>();

// Loop through each character in the string
for (char c : expression.toCharArray()) {

// If we see an opening parenthesis, push it onto the stack
if (c == '(') {
stack.push(c);
}

// If we see a closing parenthesis
else if (c == ')') {

// If the stack is empty, there was no matching '('
// Example: ")("
if (stack.isEmpty()) {
return false;
}

// Otherwise remove the matching '(' from the stack
stack.pop();
}
}

// If the stack is empty, all parentheses matched correctly
// If not empty, there are extra '(' left
return stack.isEmpty();
// TODO: Implement this method

return false;

}

/*
Expand All @@ -133,9 +217,35 @@ public static boolean isBalanced(String expression) {
*/
public static Integer mostFrequent(List<Integer> numbers) {

// Count how many times each number appears

Map<Integer, Integer> map = countFrequency(numbers);

// Track the highest frequency seen so far
int max = 0;

// Track which number has that highest frequency
Integer result = null;

// Loop through each number in the map
for (int key : map.keySet()) {

// If this number occurs more times than the current max
if (map.get(key) > max) {

// Update max frequency
max = map.get(key);

// Update the number that has the highest frequency
result = key;
}
}

// Return the number that appears most frequently
return result;
// TODO: Implement this method

return null;

}

/*
Expand All @@ -153,9 +263,32 @@ public static Integer mostFrequent(List<Integer> numbers) {
*/
public static Map<Integer, List<String>> groupByLength(List<String> words) {

// Create a map where:
// key = length of a word
// value = list of words with that length
Map<Integer, List<String>> map = new HashMap<>();

// Loop through each word in the input list
for (String word : words) {

// Find the length of the current word
int length = word.length();

// If this length is not already a key in the map
// create a new list for this length
if (!map.containsKey(length)) {
map.put(length, new ArrayList<>());
}

// Add the word to the list for its length
map.get(length).add(word);
}

// Return the map grouping words by their length
return map;
// TODO: Implement this method

return null;

}

/*
Expand All @@ -170,9 +303,31 @@ public static Map<Integer, List<String>> groupByLength(List<String> words) {
*/
public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

// Initialize a variable to keep track of the maximum sum
int maxSum = 0;

// Loops over each possible starting index of a window of size k
for (int i = 0; i <= numbers.size() - k; i++) {

// Initialize sum for the current window
int currentSum = 0;

//Sum up all numbers in the current window
for (int j = i; j < i + k; j++) {
currentSum += numbers.get(j);
}

// Update maxSum if the current window sum is bigger
if (currentSum > maxSum) {
maxSum = currentSum;
}
}

// Returns the largest sum found among all windows
return maxSum;
// TODO: Implement this method

return 0;

}
}
}

Loading