Explanation:
Traverse the string character by character and add each character to the result only if it is not already present. This keeps the first occurrence and removes duplicates.
String str = "I am in Hyderabad";
str = str.toLowerCase();
String expected = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (!expected.contains(ch + ""))
{
expected = expected + ch;
}
}
System.out.print(expected);
Explanation:
Compare each character with the remaining characters in the string. If a match is found, treat it as a duplicate and store it only once.
String str = "I am in Hyderabad";
str = str.toLowerCase();
String expected = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
for (int j = i + 1; j < str.length(); j++)
{
char ch1 = str.charAt(j);
if (ch == ch1)
{
if (!expected.contains(ch + ""))
{
expected = expected.concat(ch + "");
}
}
}
}
System.out.print(expected);
Explanation:
First extract unique characters, then count how many times each one appears in the original string.
String str = "I am in Hyderabad";
str = str.toLowerCase();
String expected = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (!expected.contains(ch + ""))
{
expected = expected + ch;
}
}
for (int i = 0; i < expected.length(); i++)
{
char x = expected.charAt(i);
int c = 0;
for (int j = 0; j < str.length(); j++)
{
if (x == str.charAt(j))
{
c++;
}
}
System.out.println(x + " came " + c + " times");
}
Explanation:
Count occurrences of each unique character and print only those whose count is greater than one.
String str = "I am in Hyderabad";
str = str.toLowerCase();
String expected = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (!expected.contains(ch + ""))
{
expected = expected + ch;
}
}
for (int i = 0; i < expected.length(); i++)
{
char x = expected.charAt(i);
int c = 0;
for (int j = 0; j < str.length(); j++)
{
if (x == str.charAt(j))
{
c++;
}
}
if (c > 1)
{
System.out.println(x + " came " + c + " times");
}
}
Explanation:
Store character frequencies in a HashMap, find the maximum value, and print all characters having that maximum count.
String str = "javav1234 java324 is good434";
char[] ch = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < ch.length; i++)
{
if (hm.containsKey(ch[i]))
{
hm.put(ch[i], hm.get(ch[i]) + 1);
}
else
{
hm.put(ch[i], 1);
}
}
int max = 0;
for (char c : hm.keySet())
{
if (hm.get(c) > max)
{
max = hm.get(c);
}
}
List<Character> maxv = new ArrayList<>();
for (char c : hm.keySet())
{
if (hm.get(c) == max)
{
maxv.add(c);
}
}
System.out.println("Max Characters " + maxv + " value is " + max);
Explanation:
Traverse each character and use Character utility methods to classify and count occurrences.
String str = "2543%#$@$fsdfdsafadsfdasf khlkj ##!#!@ DFAS DSAF";
char[] ch = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < ch.length; i++)
{
if (!Character.isLetterOrDigit(ch[i]))
{
if (!hm.containsKey(ch[i]))
{
hm.put(ch[i], 1);
}
else
{
hm.put(ch[i], hm.get(ch[i]) + 1);
}
}
}
System.out.println(hm);
Explanation:
Extract digits using modulo and division and store their frequency using a HashMap.
int num = 3214312;
int temp = num;
HashMap<Integer, Integer> hm = new HashMap<>();
while (temp != 0)
{
int d = temp % 10;
if (hm.containsKey(d))
{
hm.put(d, hm.get(d) + 1);
}
else
{
hm.put(d, 1);
}
temp = temp / 10;
}
System.out.print(hm);
Explanation:
Reverse the string, remove spaces, then rebuild the output by inserting spaces at original positions.
String original = "hi cat book";
String reverse = "";
for (int i = original.length() - 1; i >= 0; i--)
{
reverse = reverse + original.charAt(i);
}
reverse = reverse.replaceAll(" ", "");
String output = "";
int j = 0;
for (int i = 0; i < original.length(); i++)
{
if (original.charAt(i) == ' ')
{
output = output + ' ';
}
else
{
output = output + reverse.charAt(j++);
}
}
System.out.print(output);
Explanation:
Traverse each character and consider only alphabetic characters that are not vowels. Store each consonant in a HashMap and update its count.
String str = "javav1234 java324 is good434 $@!$#$#@!";
char ch[] = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < ch.length; i++)
{
if (Character.isLetter(ch[i]) &&
!(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u'))
{
if (hm.containsKey(ch[i]))
{
hm.put(ch[i], hm.get(ch[i]) + 1);
}
else
{
hm.put(ch[i], 1);
}
}
}
System.out.print(hm);
Explanation:
Split the string into words and print only those words that do not contain any vowels.
String str = "SKY fly my JAVA color";
String words[] = str.split(" ");
for (String word : words)
{
if (!word.contains("a") && !word.contains("e") &&
!word.contains("i") && !word.contains("o") &&
!word.contains("u") && !word.contains("A") &&
!word.contains("E") && !word.contains("I") &&
!word.contains("O") && !word.contains("U"))
{
System.out.print(word + " ");
}
}
// Alternative using regex
String str = "SKY fly my JAVA color";
String words[] = str.split(" ");
for (String word : words)
{
if (!word.toLowerCase().matches(".*[aeiou].*"))
{
System.out.print(word + " ");
}
}
Explanation:
Split the string into words and print only those words that do not contain any vowels.
String str = "SKY fly my JAVA color";
String words[] = str.split(" ");
for (String word : words)
{
if (!word.contains("a") && !word.contains("e") &&
!word.contains("i") && !word.contains("o") &&
!word.contains("u") && !word.contains("A") &&
!word.contains("E") && !word.contains("I") &&
!word.contains("O") && !word.contains("U"))
{
System.out.print(word + " ");
}
}
// Alternative using regex
String str = "SKY fly my JAVA color";
String words[] = str.split(" ");
for (String word : words)
{
if (!word.toLowerCase().matches(".*[aeiou].*"))
{
System.out.print(word + " ");
}
}
Explanation:
Extract digits and text separately from each word, convert the digits to a number, and print the text that many times.
String str = "12cars 17bicycles 21lorries 9a";
String words[] = str.split(" ");
for (String word : words)
{
String numbers = "";
String text = "";
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
if (Character.isDigit(ch))
{
numbers = numbers + ch;
}
else
{
text = text + ch;
}
}
int number = Integer.parseInt(numbers);
for (int i = 0; i < number; i++)
{
System.out.print(text + " ");
}
System.out.println();
}
Explanation:
First copy all zeros into a new array, then copy non-zero elements while preserving order.
int input[] = {3,4,0,5,6,0,9};
int output[] = new int[input.length];
int j = 0;
for (int i = 0; i < input.length; i++)
{
if (input[i] == 0)
{
output[j++] = 0;
}
}
for (int i = 0; i < input.length; i++)
{
if (input[i] != 0)
{
output[j++] = input[i];
}
}
for (int k = 0; k < output.length; k++)
{
System.out.print(output[k]);
}
Explanation:
Copy non-zero elements first, followed by zeros, while maintaining order.
int input[] = {3,4,0,5,6,0,9};
int output[] = new int[input.length];
int j = 0;
for (int i = 0; i < input.length; i++)
{
if (input[i] != 0)
{
output[j++] = input[i];
}
}
for (int i = 0; i < input.length; i++)
{
if (input[i] == 0)
{
output[j++] = 0;
}
}
for (int k = 0; k < output.length; k++)
{
System.out.print(output[k]);
}
Explanation:
Ignore words without digits and display text only for words containing numbers.
String str = "cars 17bicycles lorries 9a";
String words[] = str.split(" ");
for (String word : words)
{
String numbers = "";
String text = "";
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
if (Character.isDigit(ch))
{
numbers = numbers + ch;
}
else
{
text = text + ch;
}
}
if (numbers.isEmpty())
{
continue;
}
int number = Integer.parseInt(numbers);
for (int i = 0; i < number; i++)
{
System.out.print(text + " ");
}
System.out.println();
}
Explanation:
If a word does not contain a number, default it to zero to avoid errors.
String str = "cars 17bicycles lorries 9a";
String words[] = str.split(" ");
for (String word : words)
{
String numbers = "";
String text = "";
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
if (Character.isDigit(ch))
{
numbers = numbers + ch;
}
else
{
text = text + ch;
}
}
if (numbers.isEmpty())
{
numbers = numbers + 0;
}
int number = Integer.parseInt(numbers);
for (int i = 0; i < number; i++)
{
System.out.print(text + " ");
}
System.out.println();
System.out.println(numbers + " " + text + " displayed");
}
Explanation:
Print each word as many times as its length.
String str = "hi cat book";
String words[] = str.split(" ");
for (String word : words)
{
int length = word.length();
for (int i = 0; i < length; i++)
{
System.out.print(word + " ");
}
System.out.println();
System.out.println(word + " is displayed " + length + " times.");
}
Explanation:
Reverse the string, remove spaces, and rebuild the output while keeping spaces at original positions.
String original = "hi cat book";
String reverse = "";
for (int i = original.length() - 1; i >= 0; i--)
{
reverse = reverse + original.charAt(i);
}
reverse = reverse.replaceAll(" ", "");
String output = "";
int j = 0;
for (int i = 0; i < original.length(); i++)
{
if (original.charAt(i) == ' ')
{
output = output + ' ';
}
else
{
output = output + reverse.charAt(j++);
}
}
System.out.print(output);
Explanation:
Preserve vowels and spaces at original positions while reversing the remaining characters.
String original = "hi cat book";
String reverse = "";
for (int i = original.length() - 1; i >= 0; i--)
{
reverse = reverse + original.charAt(i);
}
for (int i = 0; i < reverse.length(); i++)
{
if (reverse.charAt(i)=='a'||reverse.charAt(i)=='e'||
reverse.charAt(i)=='i'||reverse.charAt(i)=='o'||
reverse.charAt(i)=='u'||reverse.charAt(i)=='A'||
reverse.charAt(i)=='E'||reverse.charAt(i)=='I'||
reverse.charAt(i)=='O'||reverse.charAt(i)=='U'||
reverse.charAt(i)==' ')
{
reverse = reverse.replace(reverse.charAt(i) + "", "");
}
}
String output = "";
int j = 0;
for (int i = 0; i < original.length(); i++)
{
if (original.charAt(i)=='a'||original.charAt(i)=='e'||
original.charAt(i)=='i'||original.charAt(i)=='o'||
original.charAt(i)=='u'||original.charAt(i)=='A'||
original.charAt(i)=='E'||original.charAt(i)=='I'||
original.charAt(i)=='O'||original.charAt(i)=='U'||
original.charAt(i)==' ')
{
output = output + original.charAt(i);
}
else
{
output = output + reverse.charAt(j++);
}
}
System.out.println(output);
Explanation:
Preserve uppercase letters and spaces at their original positions while reversing the remaining characters.
String original = "JAVA is GOOD programming LANguage";
String reverse = "";
for (int i = original.length() - 1; i >= 0; i--)
{
reverse = reverse + original.charAt(i);
}
for (int i = 0; i < reverse.length(); i++)
{
if ((reverse.charAt(i) >= 'A' && reverse.charAt(i) <= 'Z')
|| reverse.charAt(i) == ' ')
{
reverse = reverse.replace(reverse.charAt(i) + "", "");
}
}
String output = "";
int j = 0;
for (int i = 0; i < original.length(); i++)
{
if ((original.charAt(i) >= 'A' && original.charAt(i) <= 'Z')
|| original.charAt(i) == ' ')
{
output = output + original.charAt(i);
}
else
{
output = output + reverse.charAt(j++);
}
}
System.out.println(output);