Thread: Finding the most repeated names in a list

1. Member
Join Date
Aug 2009
Posts
56
Rep Power
0

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

2. 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.

3. Member
Join Date
Sep 2009
Posts
15
Rep Power
0
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.

Java Code:
```    public String getCommonName(Node head)
{
Node runner; // A pointer that will be used to traverse 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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•