Skip to content
127 changes: 113 additions & 14 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@
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) {
// You can test your methods here
testMethods();
}

public static void main(String[] args) {

// You can test your methods here
public static void testMethods() {
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);

}
System.out.println("Remove duplicates:");
System.out.println(removeDuplicates(numbers));

System.out.println("\nCount frequency:");
System.out.println(countFrequency(Arrays.asList(1, 2, 2, 3, 3, 3)));

System.out.println("\nFirst unique:");
System.out.println(firstUnique(Arrays.asList(4, 5, 1, 2, 0, 4)));

System.out.println("\nTwo Sum:");
System.out.println(twoSum(Arrays.asList(2, 7, 11, 15), 9));

System.out.println("\nUnique words:");
System.out.println(countUniqueWords(Arrays.asList("apple", "banana", "apple", "orange")));
}

/*
Problem 1
Expand All @@ -24,8 +42,8 @@ public static void main(String[] args) {
public static List<Integer> removeDuplicates(List<Integer> numbers) {

// TODO: Implement this method

return null;
Set<Integer> set = new LinkedHashSet<>(numbers);
return new ArrayList<>(set);
}

/*
Expand All @@ -39,8 +57,12 @@ public static List<Integer> removeDuplicates(List<Integer> numbers) {
public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> map = new HashMap<>();
for (Integer num : numbers) {
map.put(num, map.getOrDefault(num, 0) + 1);
}

return null;
return map;
}

/*
Expand All @@ -54,6 +76,13 @@ public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
public static Integer firstUnique(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> freqMap = countFrequency(numbers);

for (Integer num : numbers) {
if (freqMap.get(num) == 1) {
return num;
}
}

return null;
}
Expand All @@ -71,7 +100,16 @@ public static Integer firstUnique(List<Integer> numbers) {
public static boolean twoSum(List<Integer> numbers, int target) {

// TODO: Implement this method
Set<Integer> seen = new HashSet<>();

for (Integer num : numbers) {
int complement = target - num;

if (seen.contains(complement)) {
return true;
}
seen.add(num);
}
return false;
}

Expand All @@ -86,8 +124,9 @@ public static boolean twoSum(List<Integer> numbers, int target) {
public static int countUniqueWords(List<String> words) {

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

return 0;
return set.size();
}

/*
Expand All @@ -101,8 +140,17 @@ public static int countUniqueWords(List<String> words) {
public static Queue<Integer> reverseQueue(Queue<Integer> queue) {

// TODO: Implement this method
Stack<Integer> stack = new Stack<>();

return null;
while (!queue.isEmpty()) {
stack.push(queue.poll());
}

while (!stack.isEmpty()) {
queue.offer(stack.pop());
}

return queue;
}

/*
Expand All @@ -120,7 +168,20 @@ public static boolean isBalanced(String expression) {

// TODO: Implement this method

return false;
Stack<Character> stack = new Stack<>();

for (char ch : expression.toCharArray()) {
if (ch == '(') {
stack.push(ch);
} else if (ch == ')') {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}

return stack.isEmpty();
}

/*
Expand All @@ -134,8 +195,19 @@ public static boolean isBalanced(String expression) {
public static Integer mostFrequent(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> freqMap = countFrequency(numbers);

return null;
int maxCount = 0;
Integer mostFrequentNum = null;

for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
mostFrequentNum = entry.getKey();
}
}

return mostFrequentNum;
}

/*
Expand All @@ -154,8 +226,20 @@ public static Integer mostFrequent(List<Integer> numbers) {
public static Map<Integer, List<String>> groupByLength(List<String> words) {

// TODO: Implement this method
Map<Integer, List<String>> map = new HashMap<>();

for (String word : words) {
int length = word.length();

if (!map.containsKey(length)) {
map.put(length, new ArrayList<>());
}

map.get(length).add(word);
}

return map;

return null;
}

/*
Expand All @@ -172,7 +256,22 @@ public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

// TODO: Implement this method

return 0;
int windowSum = 0;

for (int i = 0; i < k; i++) {
windowSum += numbers.get(i);
}

int maxSum = windowSum;

for (int i = k; i < numbers.size(); i++) {
windowSum += numbers.get(i);
windowSum -= numbers.get(i - k);
maxSum = Math.max(maxSum, windowSum);
}

return maxSum;
}

}
}

11 changes: 10 additions & 1 deletion src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ public static void main(String[] args) {

// TODO:
// Use a for-each loop to print each student name

for (String student : students){
System.out.println(student);
}

System.out.println("\nPrinting students in uppercase:");

// TODO:
// Use a for-each loop to print each name in uppercase
for (String student : students){
System.out.println(student.toUpperCase());
}


System.out.println("\nCount the number of students:");
Expand All @@ -31,6 +36,10 @@ public static void main(String[] args) {

// TODO:
// Use a for-each loop to count how many students are in the list
for (String student : students){
count++;
}


System.out.println("Total students: " + count);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/Iterable/Examples/IteratorDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public static void main(String[] args) {
// TODO:
// Use iterator.hasNext() and iterator.next()
// Print each number

while (iterator.hasNext()){
Integer num = iterator.next();
System.out.println(num);
}

System.out.println("\nRemoving odd numbers using Iterator");

Expand All @@ -35,6 +38,13 @@ public static void main(String[] args) {
// TODO:
// Use iterator to remove odd numbers
// Remember: use iterator.remove()
while (iterator.hasNext()){
Integer num = iterator.next();

if (num % 2 != 0){
iterator.remove();
}
}


System.out.println("\nUpdated list:");
Expand Down
52 changes: 36 additions & 16 deletions src/main/java/Iterable/Practice/IterableWarmups.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static int sum(Iterable<Integer> numbers) {

// TODO:
// Use a for-each loop to calculate the sum
for (Integer num : numbers) {
total += num;
}

return total;
}
Expand All @@ -46,39 +49,56 @@ public static int countEven(Iterable<Integer> numbers) {
// TODO:
// Loop through numbers
// Increment count if number is even
for (Integer num : numbers) {
if (num % 2 == 0) {
count++;
}
}
return count;
}

return count;
}


/*
PROBLEM 3
Return the maximum value
*/
public static int findMax(Iterable<Integer> numbers) {
public static int findMax (Iterable < Integer > numbers) {

int max = Integer.MIN_VALUE;
int max = Integer.MIN_VALUE;

// TODO:
// Loop through numbers
// Update max if current number is larger
// TODO:
// Loop through numbers
// Update max if current number is larger
for (Integer num : numbers) {
if (num > max) {
max = num;
}
}

return max;
}
return max;
}


/*
PROBLEM 4 (BONUS)
Count how many times a word appears
*/
public static int countMatches(Iterable<String> words, String target) {
public static int countMatches (Iterable < String > words, String target){

int count = 0;
int count = 0;

// TODO:
// Loop through words
// Compare each word to target
// TODO:
// Loop through words
// Compare each word to target
for (String word : words) {
if (word.equals(target)) {
count++;
}
}

return count;
return count;
}
}
}


Loading