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
29 changes: 24 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,8 @@

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

public class CollectionBasics {
public static void main(String[] args) {
Expand Down Expand Up @@ -30,14 +32,13 @@ public static int sum(Collection<Integer> numbers) {

int total = 0;

// TODO:
// Loop through the collection
// Add each number to total
for (Integer num : numbers) {
total += num;
}

return total;
}


/*
PROBLEM 2
Count how many numbers are even
Expand All @@ -49,6 +50,11 @@ public static int countEven(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// If the number is even, increase count
for (Integer num : numbers){
if(num % 2==0){
count++;
}
}

return count;
}
Expand All @@ -66,6 +72,10 @@ public static int findMax(Collection<Integer> numbers) {
// Loop through numbers
// Update max if current number is larger

for (Integer num : numbers){
max = Math.max(max, num);
}

return max;
}

Expand All @@ -81,7 +91,11 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// Hint:
// Compare the size of a collection with the size of a Set

return false;
Set<Integer> set = new HashSet<>(numbers);



return set.size() == numbers.size();
}


Expand All @@ -96,6 +110,11 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// TODO:
// Loop through numbers
// If number equals target, increase count
for(int i=0; i < numbers.size(); i++){
if(numbers.contains(target)){
count++;
}
}

return count;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,44 @@ public static void main(String[] args) {
Add the following values:
10, 20, 30, 40, 50
*/
Collection<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);


/*
TODO 2:
Print the size of the numbers collection
*/
System.out.println(numbers.size());


/*
TODO 3:
Check if the collection contains 30
*/
System.out.println("Does collection contains 30? " + numbers.contains(30));


/*
TODO 4:
Remove the number 20
*/
numbers.remove(30);


/*
TODO 5:
Loop through the numbers collection
and print each value
*/
for(Integer num : numbers){
System.out.println(num);
}



/*
Expand Down
131 changes: 119 additions & 12 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package CollectionsHackerrank;

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

public class CollectionsHackerrankProblems {
public class CollectionsHackerrankPractice {
Expand All @@ -24,8 +22,10 @@ public static void main(String[] args) {
public static List<Integer> removeDuplicates(List<Integer> numbers) {

// TODO: Implement this method
HashSet<Integer> set = new HashSet<>(numbers);

return null;

return new ArrayList<>(set);
}

/*
Expand All @@ -39,8 +39,16 @@ 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 number : numbers) {
map.put(number, map.getOrDefault(number, 0) + 1);
}

return null;




return map;
}

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

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

}
// next, return the first key that value = 1
// num = numbers list
// 4 insert key check value, no.
// 5 Insert key check value, yes.
for (Integer num : numbers){
if(map.get(num) == 1){
return num;

}


}



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

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

int complement = 0;

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

if (set.contains(complement)) {
return true;
}

set.add(num);

}


return false;
}
Expand All @@ -86,8 +128,10 @@ public static boolean twoSum(List<Integer> numbers, int target) {
public static int countUniqueWords(List<String> words) {

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

return 0;

return set.size();
}

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

// TODO: Implement this method

return null;
Stack<Integer> stack = new Stack<>();
Queue<Integer> reverse = new ArrayDeque<>();
while (!(queue.isEmpty())) {
stack.push(queue.poll());

}
while (!(stack.isEmpty())){
reverse.add(stack.pop());
}
return reverse;
}

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

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

return false;
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);

if (ch == '(') {
stack.push(ch);
}
else if (ch == ')') {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}

return stack.isEmpty();
}

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

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

for(Integer number: numbers){

if (max == map.get(number)){
return number;
}
}



return null;
}
Expand All @@ -154,8 +235,21 @@ public static Integer mostFrequent(List<Integer> numbers) {
public static Map<Integer, List<String>> groupByLength(List<String> words) {

// TODO: Implement this method
HashMap<Integer, List<String>> map = new HashMap<>();
for(String word : words){
Integer length = word.length();

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

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


}


return map;
}

/*
Expand All @@ -171,8 +265,21 @@ public static Map<Integer, List<String>> groupByLength(List<String> words) {
public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

// TODO: Implement this method
int windowSum = 0;
for (int i = 0; i < k; i++) {
windowSum += numbers.get(i);
}

int maxSum = windowSum;

// 2) slide the window
for (int right = k; right < numbers.size(); right++) {
int left = right - k; // index leaving the window
windowSum = windowSum - numbers.get(left) + numbers.get(right);
maxSum = Math.max(maxSum, windowSum);
}

return 0;
return maxSum;
}
}
}
1 change: 1 addition & 0 deletions src/main/java/Hierachy/Hierarchy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class Hierarchy {
public static void main(String[] args) {


// LIST EXAMPLE
List<String> list = new ArrayList<>();
list.add("Apple");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,32 @@ public static void main(String[] args) {
// TODO:
// Use a for-each loop to print each student name

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


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

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


System.out.println("\nCount the number of students:");

int count = 0;
for(int i=0; i < students.size(); i++){
count++;
}


// TODO:
// Use a for-each loop to count how many students are in the list


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

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

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

Expand All @@ -36,6 +40,13 @@ public static void main(String[] args) {
// Use iterator to remove odd numbers
// Remember: use iterator.remove()

while (iterator.hasNext())
{
if(! (iterator.next() % 2 == 0)){
iterator.remove();
}
}


System.out.println("\nUpdated list:");
System.out.println(numbers);
Expand Down
Loading