Java / Programs
1. Write a Java program to find the last repeating character in a String.
The below program is implemented using Java 8. public class LastOccuranceOfRepeatingChar { public static void main (String [] args) { String str = "abcdklja" ; Set < Integer > myTempHashSet = new HashSet <> (); str.codePoints().filter(i -> myTempHashSet.add(i) == false ).reduce((first, second) ->...
2. Write a Java program to find the first repeating character in a String.
The below program is implemented using Java 8. public class FirstRepeatingChar { public static void main (String [] args) { String str = "abcdklha" ; Set < Integer > myTempHashSet = new HashSet <> (); str.codePoints().filter(i -> myTempHashSet.add(i) == false ).findFirst() .ifPresent(i -> System....
3. Write a simple Java program to find the sum of all integers in a List.
public class SumofIntegersOfList { public static void main (String [] args) { List < Integer > myList = Arrays.asList( 1 , 2 , 3 , 4 , 5 ); System.out.println( "Sum :" + myList.stream().reduce((i, j) -> i + j)); } }
4. Write a Java program to find the subArray in an Integer Array that has maximum Sum of the elements.
public class FindSubArrayWithMaxSum { public static void main (String [] args) { // Creating a bucket of integer array with various test values for unit testing int [][] myArr = { { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 10 , - 11 }, { 1 , 2 , 3 , 0 , 4 , 3 , - 1 , 3 , 5 , 5 , 6 , 6 , - 2 , 50 }, { ...
5. Java program to find the first non-repeating character in a String.
public class FirstNonRepeatingChar { public static void main (String [] args) { String str = "abccak" ; HashMap < Character,Boolean > myLinkedMap = new LinkedHashMap <> (); for ( int i = 0 ; i < str.length(); i ++ ) { char c = str.charAt(i); if (myLinkedMap.containsKey(c)) { myLinkedMap.put(c, fa...
6. Java program to find the last non-repeating character in a String.
public class LastNonRepeatingChar { public static void main (String [] args) { String str = "abccak" ; HashMap < Character,Boolean > myLinkedMap = new LinkedHashMap <> (); for ( int i = 0 ; i < str.length(); i ++ ) { char c = str.charAt(i); if (myLinkedMap.containsKey(c)) { myLinkedMap.put(c, fal...
7. Write a Java program to calculate Fibonacci of N using memoize recursive method.
public class Fibonacci { static int n; private int [] memoise_array = new int [ n ] ; public static void main (String [] args) { n = 6 ; System.out.println( new Fibonacci().fibonacci(n)); } int fibonacci ( int n) { if (memoise_array [ n - 1 ] != 0 ) return memoise_array [ n - 1 ] ; else if (n == ...
8. Write a Java program to count negative numbers in a 2-dimensional array which is sorted row and column wise. (Amazon interview question)
public class CountNegativeNumbers { /* * Amazon interview question */ public static void main (String [] args) { int [][] matrix = new int [][] { { - 3 , - 2 , - 1 , 0 }, { - 2 , - 1 , 0 , 1 }, { - 1 , 0 , 1 , 2 }, { - 1 , 1 , 2 , 3 } }; int count = 0 ; int k = matrix [ 0 ] .length - 1 ; for ( in...