nrsmac
nrsmac

Reputation: 105

Find largest char that occured double times in sequence

my method findLargestDoubledChar takes in a string, and returns char c when c is the largest char that appears next to it's identical code. For example: findLargestDoubledChar(look) returns o and findLargestDoubledChar(aaxx) returns x. When there is no doubles, it's supposed to return '\u0000' I have spent hours toying with this code and this is what I have. Where am I going wrong?

public static char findLargestDoubledChar(String string){

        char largestDoubleChar = '\u0000';
        String doubleChars = "";

        char[] stringArray = string.toCharArray();

        for(int x = 0; x < stringArray.length - 1; x++) {
            for (int j = x + 1; j < stringArray.length; j++) {
                if(stringArray[x] == stringArray[j]) {
                    System.out.println("Found Double!");
                } else {
                    continue;
                }
            }
        }

        for (int i = 0; i < doubleChars.length(); i++) { //find largest of doubleChars
            if (doubleChars.charAt(i) >= largestDoubleChar) {
                largestDoubleChar = doubleChars.charAt(i);
            } else {
                largestDoubleChar = '\u0000';
                continue;
            }
        }



        return largestDoubleChar;
    }

Upvotes: 0

Views: 74

Answers (4)

user4910279
user4910279

Reputation:

Try this.

static final Pattern DOUBLE_PAT = Pattern.compile("(.)\\1");

public static char findLargestDoubledChar(String string){
    return DOUBLE_PAT.matcher(string).results()
        .map(x -> x.group().charAt(0))
        .max(Comparator.naturalOrder()).orElse('\u0000');
}

Upvotes: 0

Andreas
Andreas

Reputation: 159086

It can be done in a single statement like this, which also supports Unicode characters from the supplemental planes, such as emojis.

public static String findLargestDoubledChar(String input) {
    return Character.toString(Pattern.compile("(.)\\1").matcher(input)
            .results().mapToInt(r -> r.group(1).codePointAt(0)).max().orElse('\0'));
}

Test

System.out.println(findLargestDoubledChar("look"));
System.out.println(findLargestDoubledChar("aaxx"));
System.out.println(findLargestDoubledChar("šŸ˜€šŸ˜€šŸ˜šŸ˜šŸ˜’šŸ˜€šŸ˜€"));

Output

o
x
šŸ˜

Upvotes: 0

Jacobious
Jacobious

Reputation: 3

You have a few problems.

  1. The inner loop isn't needed for the first loop.
  2. You aren't storing the doubled char when you find it. Example answer from modifying yours:

    ' public static char findLargestDoubledChar(String string){

    char largestDoubleChar = '\u0000';
    String doubleChars = "";
    
    char[] stringArray = string.toCharArray();
    
    for(int x = 0; x < stringArray.length - 1; x++) {
        if (stringArray[x]==stringArray[x+1]){
            System.out.println("Found Double");
            doubleChars= doubleChars+ stringArray[x];
    
        }
    }
    
    for (int i = 0; i < doubleChars.length(); i++) { //find largest of doubleChars
        if (doubleChars.charAt(i) >= largestDoubleChar) {
            largestDoubleChar = doubleChars.charAt(i);
        } else {
            largestDoubleChar = '\u0000';
            continue;
        }
    }
    
    
    
    return largestDoubleChar;
    

    }'

Upvotes: 0

John Lee
John Lee

Reputation: 11

Looks like you're iterating over doubleChars in the second loop when it's being set to an empty string. Maybe something like this would work better:

public static char findLargestDoubledChar(String string){

    char largestDoubleChar = '\u0000';
    String doubleChars = "";

    char[] stringArray = string.toCharArray();

    for(int i = 0; i < stringArray.length - 1; i++) {
        if (stringArray[i] == stringArray[i + 1]) {
            System.out.println("Found Double!");
            if (stringArray[i] > largestDoubleChar) largestDoubleChar = stringArray[i];
        }
    }

    return largestDoubleChar;
}

Upvotes: 1

Related Questions