Solutions to Programming Questions
import java.util.*;
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 (!set.add(num)) 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 < arr.length; 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) set1.add(num);
for (int num : arr2) if (set1.contains(num)) result.add(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 = s1.toCharArray(), arr2 = s2.toCharArray();
Arrays.sort(arr1); Arrays.sort(arr2);
return Arrays.equals(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 : str.toCharArray()) countMap.put(c, countMap.getOrDefault(c, 0) + 1);
for (Map.Entry<Character, Integer> entry : countMap.entrySet())
if (entry.getValue() == 1) return entry.getKey();
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 = freqMap.getOrDefault(num, 0) + 1;
freqMap.put(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 (!result.contains(num)) result.add(num);
}
return result;
}
// 3. Sort a map by values
public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
list.sort(Map.Entry.comparingByValue());
Map<String, Integer> sortedMap = new LinkedHashMap<>();
for (Map.Entry<String, Integer> entry : list) sortedMap.put(entry.getKey(), entry.getValue());
return sortedMap;
}
public static void main(String[] args) {
// Sample test cases
int[] arr = {3, 5, 2, 5, 7, 8, 1};
System.out.println("Second Largest: " + secondLargest(arr));
System.out.println("Contains Duplicate: " + containsDuplicate(arr));
}
}