JAVA Programs
1. Merge Two Arrays Into Single Sorted Array Without Duplicates In Java
{
//Step 1 : Merging of two arrays
int[] mergedArray = new int[[Link] + [Link]];
int i=0, j=0, k=0;
while (i < [Link])
{
mergedArray[k] = arrayA[i];
k++;
i++;
}
while (j < [Link])
{
mergedArray[k] = arrayB[j];
k++;
j++;
}
//Step 2 : Removing duplicates from merged array
//Remember, HashSet allows only unique elements
Set<Integer> setWithNoDuplicates = new HashSet<>();
for (int m = 0; m < [Link]; m++) {
[Link](mergedArray[m]);
}
Iterator<Integer> it = [Link]();
int[] mergedArrayWithNoDuplicates = new
int[[Link]()];
int n = 0;
while ([Link]())
{
mergedArrayWithNoDuplicates[n] = [Link]();
n++;
}
//Step 3 : Sorting merged array after removing duplicates
[Link](mergedArrayWithNoDuplicates);
return mergedArrayWithNoDuplicates;
}
2. Right Angle Triangle
import [Link].*;
// Java code to demonstrate right star triangle
public class GeeksForGeeks {
// Function to demonstrate printing pattern
public static void StarRightTriangle(int n)
{
int a, b;
// outer loop to handle number of rows
// k in this case
for (a = 0; a < n; a++) {
// inner loop to handle number of columns
// values changing acc. to outer loop
for (b = 0; b <= a; b++) {
// printing stars
[Link]("* ");
}
// end-line
[Link]();
}
}
// Driver Function
public static void main(String args[])
{
int k = 5;
StarRightTriangle(k);
}
}
3. Reverse String of Words
public class ReverseWords {
public static void main(String[] args) {
// Input string
String input = "today its raining";
// Reverse words
String output = reverseWords(input);
// Print the output
[Link](output);
}
public static String reverseWords(String input) {
// Split the input string into words
String[] words = [Link](" ");
// Use StringBuilder to construct the reversed string
StringBuilder reversed = new StringBuilder();
// Loop through the words array in reverse order
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i]);
if (i != 0) {
[Link](" "); // Add a space between words
}
}
return [Link]();
}
}
4. Reverse of a number
// Java program to reverse a number
import [Link].*;
// Driver Class
class GFG {
// Function to reverse the number
static int reverse(int n)
{
int rev = 0;
int rem;
while (n > 0) {
rem = n % 10;
rev = (rev * 10) + rem;
n = n / 10;
}
return rev;
}
// Driver Function
public static void main(String[] args)
{
int n = 4526;
[Link]("Reversed Number is "+ reverse(n));
}
}
5. Reverse of a String
import [Link].*;
import [Link];
class GFG {
public static void main(String[] args) {
String s = "Geeks";
String r = "";
char ch;
for (int i = 0; i < [Link](); i++) {
// extracts each character
ch = [Link](i);
// adds each character in
// front of the existing string
r = ch + r;
}
[Link](r);
}
}
6. Duplicate elements of an array
public class DuplicateBruteForce {
public static void findDuplicates(int[] array) {
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (array[i] == array[j]) {
[Link]("Duplicate element found:
" + array[i]);
break;
}
}
}
}
public static void main(String[] args) {
int[] array = {4, 5, 6, 7, 5, 6};
findDuplicates(array);
}
}
7. Count duplicates in an array
class CountDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 1, 2, 4, 5, 2};
[Link]("Duplicate elements:");
for (int i = 0; i < [Link]; i++) {
int count = 1;
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
count++;
arr[j] = -1; // Mark duplicate as -1
}
}
if (count > 1 && arr[i] != -1) {
[Link](arr[i] + " occurs " + count +
" times");
}
}
}
}
8. Frequency of characters in a string while maintaining their order of
occurrence
public class OrderOfOccurrence {
public static void main(String[] args) {
String input = "Hello World";
char[] chars = [Link]();
for (int i = 0; i < [Link]; i++) {
int count = 1; // Initialize count
if (chars[i] == '\0') continue; // Skip already counted
characters
// Count occurrences of chars[i]
for (int j = i + 1; j < [Link]; j++) {
if (chars[i] == chars[j]) {
count++;
chars[j] = '\0'; // Mark as counted
}
}
[Link](chars[i] + ": " + count);
}
}
}
9. Sorting an array without using built-in functions
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 5, 6};
// Bubble Sort Algorithm
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print the sorted array
[Link]("Sorted array: ");
for (int num : arr) {
[Link](num + " ");
}
}
}
10. Java program for identifying vowels
public class VowelIdentifier {
public static void main(String[] args) {
String input = "Hello World";
[Link]("Vowels: ");
for (char c : [Link]().toCharArray()) {
if ("aeiou".contains([Link](c))) {
[Link](c + " ");
}
}
}
}