Arrays: read, index, return, sort
int[] a = {4, 2, 6};
int first = a[0];
int n = a.length;
Arrays.sort(a); // ascending, in place
return new int[]{i, j}; // return two indices
HashMap: count and look up
Map<String,Integer> count = new HashMap<>();
count.put("Ali", 1);
count.getOrDefault("Ali", 0); // 0 if missing
count.containsKey("Ali"); // true / false
for (String key : count.keySet())
System.out.println(key + " " + count.get(key));
Stack: last in, first out
Deque<Character> st = new ArrayDeque<>();
st.push('(');
st.peek(); // look, don't remove
st.pop(); // remove the top
st.isEmpty();
Comparing strings: never with ==
a.equals(b); // same letters?
a.compareTo(b); // negative = a comes first
Two traps that cost submissions
a == b on String compares references, not letters. Always .equals()
- Factorials and big sums overflow
int silently. Use long