11. Remove all even numbers from a list.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
[1, 3, 5]
Code In Java
import java.util.*;
public class Main {
public static List removeEvenNumbers() {
List lst = Arrays.asList(1, 2, 3, 4, 5);
// Your code here
return lst;
}
}
Output
Click Run Button to view compiled output
12. Find the maximum product of two numbers in an array.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
20
Code In Java
import java.util.*;
public class Main {
public static int maxProduct() {
int[] arr = {1, 2, 3, 4, 5};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
13. Find the common elements in three lists.
Required Input:
[1, 2, 3], [2, 3, 4], [3, 4, 5]
Expected Output:
[3]
Code In Java
import java.util.*;
public class Main {
public static List commonInThreeLists() {
List lst1 = Arrays.asList(1, 2, 3);
List lst2 = Arrays.asList(2, 3, 4);
List lst3 = Arrays.asList(3, 4, 5);
// Your code here
return lst1;
}
}
Output
Click Run Button to view compiled output
14. Find the missing number in an array from 1 to n.
Required Input:
[1, 2, 4, 5], n = 5
Expected Output:
3
Code In Java
import java.util.*;
public class Main {
public static int findMissingNumber() {
int[] arr = {1, 2, 4, 5};
int n = 5;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
15. Find all pairs in an array that sum to a target value.
Required Input:
[1, 2, 3, 4, 5], target = 5
Expected Output:
[[2, 3], [1, 4]]
Code In Java
import java.util.*;
public class Main {
public static List
- > findPairs() {
int[] arr = {1, 2, 3, 4, 5};
int target = 5;
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
16. Remove duplicates from a string while maintaining the order of characters.
Required Input:
programming
Expected Output:
progamin
Code In Java
import java.util.*;
public class Main {
public static String removeDuplicates() {
String str = "programming";
// Your code here
return str;
}
}
Output
Click Run Button to view compiled output
17. Find the first non-repeating character in a string.
Required Input:
"swiss"
Expected Output:
w
Code In Java
import java.util.*;
public class Main {
public static char firstNonRepeatingCharacter() {
String str = "swiss";
// Your code here
return ' ';
}
}
Output
Click Run Button to view compiled output
18. Rotate a 2D matrix 90 degrees clockwise.
Required Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected Output:
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]
Code In Java
import java.util.*;
public class Main {
public static int[][] rotateMatrix() {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Your code here
return matrix;
}
}
Output
Click Run Button to view compiled output
19. Find the element that appears only once in an array where every other element appears twice.
Required Input:
[1, 2, 2, 3, 3]
Expected Output:
1
Code In Java
import java.util.*;
public class Main {
public static int singleElement() {
int[] arr = {1, 2, 2, 3, 3};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
20. Find the majority element (appears more than n/2 times) in an array.
Required Input:
[3, 3, 4, 2, 3, 3, 3]
Expected Output:
3
Code In Java
import java.util.*;
public class Main {
public static int majorityElement() {
int[] arr = {3, 3, 4, 2, 3, 3, 3};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output