Solutions to Programming Questions
import [Link].*;
public class Solutions {
// Arrays Solutions
// 1. Find the second largest element in an array
public static int secondLargest(int[] arr) {
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
return second;
}
// 2. Check if an array contains a duplicate
public static boolean containsDuplicate(int[] arr) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
if () return true;
}
return false;
}
// 3. Find the missing number in an array
public static int findMissingNumber(int[] arr, int n) {
int sum = n * (n + 1) / 2;
for (int num : arr) sum -= num;
return sum;
}
// 4. Move all zeros to the end of the array
public static void moveZerosToEnd(int[] arr) {
int j = 0;
for (int i = 0; i < [Link]; i++) {
if (arr[i] != 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j++] = temp;
}
}
}
// 5. Find the intersection of two arrays
public static Set<Integer> arrayIntersection(int[] arr1, int[] arr2) {
Set<Integer> set1 = new HashSet<>(), result = new HashSet<>();
for (int num : arr1) [Link](num);
for (int num : arr2) if ([Link](num)) [Link](num);
return result;
}
// Strings Solutions
// 1. Reverse a string
public static String reverseString(String str) {
return new StringBuilder(str).reverse().toString();
}
// 2. Check if two strings are anagrams
public static boolean areAnagrams(String s1, String s2) {
char[] arr1 = [Link](), arr2 = [Link]();
[Link](arr1); [Link](arr2);
return [Link](arr1, arr2);
}
// 3. Find the first non-repeating character in a string
public static char firstNonRepeatingChar(String str) {
Map<Character, Integer> countMap = new LinkedHashMap<>();
for (char c : [Link]()) [Link](c, [Link](c, 0) + 1);
for ([Link]<Character, Integer> entry : [Link]())
if ([Link]() == 1) return [Link]();
return '_';
}
// Collections Solutions
// 1. Find the most frequent element in a list
public static int mostFrequentElement(List<Integer> list) {
Map<Integer, Integer> freqMap = new HashMap<>();
int maxCount = 0, res = -1;
for (int num : list) {
int count = [Link](num, 0) + 1;
[Link](num, count);
if (count > maxCount) {
maxCount = count;
res = num;
}
}
return res;
}
// 2. Remove duplicates from a list without using Set
public static List<Integer> removeDuplicates(List<Integer> list) {
List<Integer> result = new ArrayList<>();
for (int num : list) {
if () [Link](num);
}
return result;
}
// 3. Sort a map by values
public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
List<[Link]<String, Integer>> list = new ArrayList<>([Link]());
[Link]([Link]());
Map<String, Integer> sortedMap = new LinkedHashMap<>();
for ([Link]<String, Integer> entry : list) [Link]([Link](), [Link]());
return sortedMap;
}
public static void main(String[] args) {
// Sample test cases
int[] arr = {3, 5, 2, 5, 7, 8, 1};
[Link]("Second Largest: " + secondLargest(arr));
[Link]("Contains Duplicate: " + containsDuplicate(arr));
}
}