Cops and robbers program to find the most frequent character in a string I made this program to train me for a competition. The hypothetical question I've used was 


  The cops need help finding who is the most probable criminal to commit a crime.
  Witnesses tell them multiple different people, making it hard to find the criminal.
  Use your knowledge of coding and the one-letter identifications provided for the criminals
  to find who most likely did it and print.


It was fun making the code; however, it felt like it took too long for such a simple code. Is there any way to make this simpler or better?

import java.util.*;

public class CopsAndRobbers {

    public static void main(String[] args) {
        //The cops need help finding who is the most probable criminal to commit a crime
        //Witnesses tell them multiple different people, making it hard to find the criminal
        //Use your knowledge of coding and the one-letter identifications provided for the criminals
        //To find who most likely did it and print.

        TreeSet&lt;String&gt; done = new TreeSet&lt;String&gt;();
        ArrayList&lt;String&gt; all = new ArrayList&lt;String&gt;();
        ArrayList&lt;Integer&gt; num = new ArrayList&lt;Integer&gt;();
        ArrayList&lt;String&gt; some = new ArrayList&lt;String&gt;();
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        for(int i = 0; i &lt; input.length(); i++)
        {
            done.add(String.valueOf(input.charAt(i)));
            all.add(String.valueOf(input.charAt(i)));
        }
        int amount = done.size();
        for(int i = 0; i &lt; amount; i++)
        {
            String letter = done.first();
            int tempNum = 0;
            for(int j = 0; j &lt; all.size(); j++)
            {
                if(all.get(j).equals(letter))
                {
                    tempNum++;
                }
            }
            num.add(tempNum);
            some.add(letter);
            done.remove(letter);
        }
        int highest = 0;
        int overallIndex = 0;
        for(int i = 0; i &lt; num.size(); i++)
        {
            if(num.get(i) &gt; highest)
            {
                highest = num.get(i);
                overallIndex = i;
            }
        }
        System.out.println("The most likely criminal is " + some.get(overallIndex) + " with " + highest + " witnesses.");
    }

}




Sample Input

aggdhaamfa


Sample Output

The most likely criminal is a with 4 witnesses.
for(int i = 0; i &lt; input.length(); i++)
{
    done.add(String.valueOf(input.charAt(i)));
    all.add(String.valueOf(input.charAt(i)));
}


Instead of iterating over the input string like that, converting every single character to a new String, actually doing all of this twice, and adding each to a collection, you can convert the string to a list of Strings (for each character) like this:

List&lt;String&gt; inputChars = Arrays.asList(input.split(""));


Note that previously to Java 8 this results in the first element being an empty string, which must be handled accordingly. 

Then add all of the characters in the array to the collections:

done.addAll(inputChars);
all.addAll(inputChars);




int highest = 0;
int overallIndex = 0;
for(int i = 0; i &lt; num.size(); i++)
{
    if(num.get(i) &gt; highest)
    {
        highest = num.get(i);
        overallIndex = i;
    }
}


To better structure your code, move the loop to a method findIndexOfHighest(). Your overallIndex actually is the indexOfHighest. Then you get the index of the highest element and the highest element like this:

int indexOfHighest = findIndexOfHighest();
int highest = num.get(indexOfHighest);




Add blank lines between blocks of code, e. g. between for loops. That makes your code much more readible.



int amount = done.size();
for(int i = 0; i &lt; amount; i++)
{
    String letter = done.first();
    int tempNum = 0;
    for(int j = 0; j &lt; all.size(); j++)
    {
        if(all.get(j).equals(letter))
        {
            tempNum++;
        }
    }
    num.add(tempNum);
    some.add(letter);
    done.remove(letter);
}


Here the inner loop can be a for-each loop. Also it should be in a separate method, because what it does is countOccurences(String s, char c). The variable tempNum should be called count or occurences.