# Finding the most repeated names in a list

• 09-15-2009, 12:22 PM
jboy
Finding the most repeated names in a list
Can Someone please give me an idea about how to find the most common name in a linkedlist of car names?
Thank you
• 09-15-2009, 12:49 PM
Fubarable
1) Figure out the steps that you would take if you were doing this on paper.
2) Translate the algorithm to Java.

and that's it! Seriously, that's the best way to work this out.
• 09-17-2009, 03:08 PM
THEAniKan
Quote:

1) Figure out the steps that you would take if you were doing this on paper.
2) Translate the algorithm to Java.
thats the way to do it.

Except i'll make it easier for you.

Code:

```    public String getCommonName(Node head)     {         Node runner; // A pointer that will be used to traverse the list.         runner = head; // Start with runner pointing to the head of the list.         HashMap<String,Integer> hash = new HashMap<String,Integer>(); // Keeps track of how many of each name there is.         String[] names = new String[1000]; // Keeps track of all the names.         int cnt = 0; // Keeps track of how many names there are currently encountered.         while ( runner != null )  // Continue until null is encountered.         {             if(hash.containsKey(runner.name)) // IF the name was already encountered...                 hash.put(runner.name,hash.get(runner.name)+1); // THEN add one to the name count             else // OTHERWISE...             {                 names[cnt] = runner.name; // Add the name to the list of names.                 cnt++; // Add one to the counter.                 hash.put(runner.name,1); // And then set the count for that name to 1             }             runner = runner.next; // Move on to the next node in the list.         }         String s = "";         int max = 0; // The biggest number so far         String maxname = ""; // The name of the biggest number         for(int i = 0; i < cnt; i++)         {             int namecount = hash.get(names[i]); // We get the amount of names for this name             if(namecount > max) // IF its bigger than anything we've encountered before...             {                 max = namecount; // THEN set the biggest number...                 maxname = names[i]; // ...and value to the current             }         }         return maxname; // Return the name that appears the most     }```
This took a while to code, so lemme know if it works.