How to program to print first non repeated character from String?
One of the most common string interview questions: Find the first non-repeated (unique) character in a given string. for Example, if given String is "Morning" then it should print "M". This question demonstrates the efficient use of the hash table data structure.
public class Main {
static String str = "MMorning";
static char c[] = str.toCharArray();
public static void main(String[] args) {
for (int i = 0; i < c.length; i++) {
char check = c[i];
int number = 0;
for (int j = 0; j < c.length; j++) {
if (check == c[j]) {
number++;
}
if (number > 1) {
break;
}
}
if (number == 1) {
System.out.println(check);
break;
}
}
}
}
public class Main {
static String str = "MMorning";
static char c[] = str.toCharArray();
public static void main(String[] args) {
for (int i = 0; i < c.length; i++) {
char check = c[i];
int number = 0;
for (int j = 0; j < c.length; j++) {
if (check == c[j]) {
number++;
}
if (number > 1) {
break;
}
}
if (number == 1) {
System.out.println(check);
break;
}
}
}
}
Comments
Post a Comment