Sumasoftprooogram
Sumasoftprooogram
Basic Programs
---
2. String-Based Programs
4. Reverse a string
---
3. Number-Based Programs
---
4. Array-Based Programs
import java.util.HashSet;
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 5, 1};
HashSet<Integer> set = new HashSet<>();
System.out.print("Duplicate Elements: ");
for (int num : arr) {
if (!set.add(num))
System.out.print(num + " ");
}
}
}
---
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 2, 8};
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {2, 3, 4, 10, 40};
int key = 10, left = 0, right = arr.length - 1;
Arrays.sort(arr);
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
System.out.println("Element found at index: " + mid);
return;
} else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
System.out.println("Element not found");
}
}