1.
TO COUNT FREQUENCY USING HASHMAP-[use of
getOrDefault() function]
for(int i=0;i<n;i++)
Cool.put(arr[i], cool.getOrDefault(arr[i],0)+1);
}
2. TO CONVERT CHAR TO INT;
Int n=ch-‘0’; //where ch is a character
OR
char ch = 'A';
int ascii = (int) ch; // ASCII value of 'A' (65)
3. CREATING LIST OF LIST-
for (int r = 1; r <= k; r++) {
groups[r] = new ArrayList<>();
}
4. WAY TO BREAK DIRECTLY OUT OF OUTTER LOOP-
outer: for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
long xor = arr[i] ^ arr[j];
product *= xor;
if (product > r) {
productOverflow = true;
break outer;
RECURSSION-
FIND SQUARE OF ANY NUMBER
import java.util.*;
public class Main{
public static double myPow(double x, int n) {
double ans = 1.0;
long nn = n;
if (nn < 0) nn = -1 * nn;
while (nn > 0) {
if (nn % 2 == 1) {
ans = ans * x;
nn = nn - 1;
} else {
x = x * x;
nn = nn / 2;
if (n < 0) ans = (double)(1.0) / (double)(ans);
return ans;
public static void main(String args[])
System.out.println(myPow(2,10));
CREATING LIST OF LIST IN JAVA-
List<List<Integer>> listOfLists = new ArrayList<>();
Java stores references to listing inside listOfLists, not a copy. So if you
clear listing, the list inside listOfLists also becomes empty — because they
are the same object in memory.
PASS BY VALUE AND PASS BY REFERENCE IN JAVA FOR
RECURSSION
Java doesn't support passing primitive types by reference, but arrays and
objects are passed by reference. So we can pass an int[] count = new
int[1]; and update count[0] inside the recursive method.