How to make a program that returns the first recurring character in a string in Java? [closed]
How to make a program that returns the first recurring character in a string in Java? [closed]
I got a school assignment in computers in which I have to make a small program that returns the first recurring characters in a string using loops and conditional statements but I can't figure out how to so. Please help!
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Please show us what you've got so far.
– user8035311
Jul 2 at 10:05
I'm voting to close this question as off-topic because it does not show any effort and instead is a request for writing code.
– manish
Jul 2 at 10:23
@Hearen Didn't downvote, but the question as it is now is too broad and thus off-topic. It should be closed instead of being answered. Helpful link: How do I ask and answer homework questions?
– Modus Tollens
Jul 2 at 10:40
An answer that contains only code, and very little in the way of explanation should always expect downvotes. If you don't clearly explain what you're doing, nobody's going to learn anything from your code.
– Dawood ibn Kareem
Jul 2 at 10:57
3 Answers
3
The following code will do so:
String text = ""; // String that holds the text
int length = text.length();
outerloop:
for (int i = 0; i <= length - 1; i++) {
char curChar = text.charAt(i);
for (int j = i + 1; j <= length - 1; j++) {
if (curChar == text.charAt(j)) {
System.out.println("The first recurring character in string " + text + " is '" + curChar + "'.");
break outerloop;
}
}
}
But try doing things yourself!
Since it is a school assignment, I am not sure if you're going to be allowed to have the full code, but I'm sure this will at least help
Pseudo code:
string = "asda"
foreach letter in string DO
temp = string
temp.remove_char_from_string(letter)
if (letter in temp = true)
OUTPUT string.indexOf(letter)
exit foreach
endif
endfor
Why do you bother checking
if (letter in string = true)
when you're only iterating through letters that are in the string?– Dawood ibn Kareem
Jul 2 at 11:02
if (letter in string = true)
Well that ain't java, since I remember in school we were never allowed to research direct answers, so here in pseudo code I'm trying to explain, for each letter in the string, search for the letter again after the first has been removed
– user8894408
Jul 2 at 11:30
@Dawood ibn Karen if the letter repears, it means it's recurring.
– user8894408
Jul 2 at 11:36
Well, yes, since you edited your answer after my original comment, it makes more sense. I've removed my downvote, because it now looks like something that would actually work. You probably want to break out of the loop as soon as a recurring letter is found, though, since the question is just to find the first such letter.
– Dawood ibn Kareem
Jul 2 at 19:34
@Dawood ibn Kareem yes I agree and apologies for not making it clear in the first place
– user8894408
Jul 2 at 20:39
In Java 8, you can do it easily as:
public class HelloWorld {
public static void main(String... args) {
String s = "Hi, there";
Map<Character, Long> characterIntegerMap = s.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Character::valueOf, Collectors.counting()));
for (int i = 0, len = s.length(); i < len; ++i) {
Character c = s.charAt(i);
if (characterIntegerMap.get(Character.valueOf(c)) > 1) {
System.out.println(c);
break;
}
}
}
}
If you'd prefer a simpler version, you can just ignore map
used here instead using this, which might tumble into performance issues when string is large:
map
for (int i = 0, len = s.length(); i < len; ++i) {
Character c = s.charAt(i);
if (i != s.lastIndexOf(c)) {
System.out.println(c);
break;
}
}
Possible duplicate of how to find the first recurrring character in a string using java 8 stream without intermediate terminal operation
– Joshua Simon Bloom
Jul 2 at 10:05