Skip to content
Advertisement

Method that scans array for most common word length, but sends error message if two different elements have the same length

I have a method that takes in an array (created by Scanner input in main) and sorts the array to find the most common words before printing the length of those words. However, I’m also trying to have an error message pop up if two different elements have the same length; this is where I’m having trouble getting started.

Method:

JavaScript

Any suggestions?

Advertisement

Answer

  1. String comparison – arr[i] == arr[i-1] does not work. Each string object is different for Java. Use arr[i].equals(arr[i-1]).
  2. When you check count > maxCount, it means there is definitely a string more frequent than the previous one. However, what if count == maxCount? – It would mean you have found two strings with equal frequency.

Use a flag that tells you if there are multiple max frequency strings or not. So now we are going to check both for greater and equal conditions and based on what is true, we are going to set a flag unique.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement