The document contains a collection of 40 Java coding problems focused on string and array manipulation, utilizing Java Collections and Streams. Each problem includes a method implementation that addresses a specific task, such as counting character frequency, checking for anagrams, and finding duplicates in arrays. The solutions demonstrate various programming techniques and best practices in Java.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Top50_Java_String_Array_Problems
The document contains a collection of 40 Java coding problems focused on string and array manipulation, utilizing Java Collections and Streams. Each problem includes a method implementation that addresses a specific task, such as counting character frequency, checking for anagrams, and finding duplicates in arrays. The solutions demonstrate various programming techniques and best practices in Java.
public static int findMissing(int[] arr, int n) { int total = IntStream.rangeClosed(1, n).sum(); int sum = Arrays.stream(arr).sum(); return total - sum; }
// 17. Second highest number
public static int secondHighest(int[] arr) { return Arrays.stream(arr).boxed().distinct() .sorted(Comparator.reverseOrder()).skip(1).findFirst().orElseThrow(); }
// 18. Group even and odd
public static Map<Boolean, List<Integer>> groupEvenOdd(int[] arr) { return Arrays.stream(arr).boxed().collect(Collectors.partitioningBy(n -> n % 2 == 0)); }
public static Optional<Integer> majorityElement(int[] arr) { int n = arr.length; return Arrays.stream(arr).boxed() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet().stream() .filter(e -> e.getValue() > n / 2) .map(Map.Entry::getKey).findFirst(); }
// 21. Find all pairs with a given sum
public static List<List<Integer>> findPairsWithSum(int[] arr, int target) { Set<Integer> seen = new HashSet<>(); List<List<Integer>> result = new ArrayList<>(); for (int num : arr) { int complement = target - num; if (seen.contains(complement)) { result.add(Arrays.asList(complement, num)); } seen.add(num); } return result; }
// 22. Rotate array to the right by k steps
public static int[] rotateRight(int[] arr, int k) { int n = arr.length; return IntStream.range(0, n) .map(i -> arr[(n - k + i) % n]) .toArray(); }
// 23. Check if array contains a number
public static boolean contains(int[] arr, int key) { return Arrays.stream(arr).anyMatch(x -> x == key); }
public static int kthSmallest(int[] arr, int k) { return Arrays.stream(arr).sorted().skip(k - 1).findFirst().orElseThrow(); }
// 27. Find all palindromic substrings
public static List<String> palindromicSubstrings(String str) { Set<String> result = new HashSet<>(); for (int i = 0; i < str.length(); i++) { for (int j = i + 1; j <= str.length(); j++) { String sub = str.substring(i, j); if (isPalindrome(sub)) result.add(sub); } } return new ArrayList<>(result); }
// 28. Find longest palindromic substring
public static String longestPalindrome(String str) { String res = ""; for (int i = 0; i < str.length(); i++) { for (int j = i + 1; j <= str.length(); j++) { String sub = str.substring(i, j); if (isPalindrome(sub) && sub.length() > res.length()) res = sub; } } return res; }