-4

How could an user input their numbers, for example : "122113333443" and I needed to output "3333".

If they again input something like "1224" I then needed to output "22".

How would this be possible if I don't know which numbers they are going to input and how the code would look like? So far I only have the beginning, which shows input output error if the input aren't numbers.

    int k;

    Scanner sc = new Scanner(System.in);
    System.out.println("input string:");
    if (sc.hasNextInt())
        k = sc.nextInt();
    else {
        System.out.println("input-output error");
        sc.close();
        return;
    }
Denijs
  • 9
  • 3

2 Answers2

0

This is Java not JavaScript, wrong tag. However, heres my shot. I am not that good with java but anyway:

public String findMostCommon(int inp) {
        int[] occuranceTable = new int[9];
        for (int i = 0; i < occuranceTable.length; i++) {
            occuranceTable[i] = 0;
        }
        
        // Convert input int to Array of digits
        String temp = Integer.toString(inp);
        int[] digits = new int[temp.length()];
        for (int i = 0; i < temp.length(); i++) {
            digits[i] = temp.charAt(i) - '0';
        }
        
        // Get Occurances of each digit
        for(int digit : digits) {
            occuranceTable[digit]++;
        }
        
        // Find most frequent digit
        int max = -1;
        for (int i = 0; i < occuranceTable.length; i++) {
            max = occuranceTable[i] > max ? i : max;

        }
        
        // Make result string
        String result = "";
        for (int i = 0; i < occuranceTable[max]; i++)
            result += String.valueOf(max);
            
        return result;
        
}
Faraz
  • 433
  • 3
  • 7
0

You can use a variable (StringBuilder longest in the code given below) to keep track of the longest sequence of characters having the same characters.

Iterate all characters of the string and keep appending the characters to a StringBuilder (StringBuilder sb in the code given below) until a different character is found. When this happens, reset the sb.

After each append to the sb, check if its length has become bigger than that of longest. If yes, transfer the content of the StringBuilder to longest.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Input string: ");
        String input = sc.nextLine();
        System.out.println(getLongest(input));
    }

    static String getLongest(String str) {
        StringBuilder sb = new StringBuilder();
        StringBuilder longest = new StringBuilder();
        char current = 0;

        // Process all but the last character of str
        for (int i = 0; i < str.length() - 1; i++) {
            current = str.charAt(i);
            sb.append(current);
            if (sb.length() > longest.length()) {
                longest = new StringBuilder(sb);
            }
            if (current != str.charAt(i + 1)) {
                sb = new StringBuilder();
            }
        }

        // Process the last character of str
        sb.append(str.charAt(str.length() - 1));
        if (sb.length() > longest.length()) {
            longest = new StringBuilder(sb);
        }

        return longest.toString();
    }
}

A sample run:

Input string: 122113333443
3333

Another sample run:

Input string: 1224
22
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You can use `String` instead of `StringBuilder` but I recommend you use `StringBuilder` instead of `String` for such a case because repeated string concatenation in a loop creates additional as many instances of `String` as the number of concatenation. Check [this discussion](https://stackoverflow.com/questions/7817951/string-concatenation-in-java-when-to-use-stringbuilder-and-concat) to learn more about it. – Arvind Kumar Avinash Dec 09 '20 at 10:41
  • How could I transfer "System.out.println(getLongest("122113333443"));" to System.out.println((and in here user input)) – Denijs Dec 09 '20 at 10:42
  • @Denijs - Updated to make it interactive. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Dec 09 '20 at 10:46