I made this program to train me for a competition. The hypothetical question I’ve used was the one in the comments near the top of the code. 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<String> done = new TreeSet<String>(); ArrayList<String> all = new ArrayList<String>(); ArrayList<Integer> num = new ArrayList<Integer>(); ArrayList<String> some = new ArrayList<String>(); @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); String input = sc.nextLine(); for(int i = 0; i < 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 < amount; i++) { String letter = done.first(); int tempNum = 0; for(int j = 0; j < 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 < num.size(); i++) { if(num.get(i) > highest) { highest = num.get(i); overallIndex = i; } } System.out.println("The most likely criminal is " + some.get(overallIndex) + " with " + highest + " witnesses."); } }