Results 1 to 3 of 3
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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. #3
    THEAniKan is offline Member
    Join Date
    Sep 2009
    Rep Power


    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.
            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( // IF the name was already encountered...
                    hash.put(,hash.get(; // THEN add one to the name count
                else // OTHERWISE...
                    names[cnt] =; // Add the name to the list of names.
                    cnt++; // Add one to the counter.
                    hash.put(,1); // And then set the count for that name to 1
                runner =; // 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.

Similar Threads

  1. Finding objects in a list
    By starwars in forum AWT / Swing
    Replies: 5
    Last Post: 09-11-2009, 03:42 PM
  2. output for list of names in a format
    By Ms.Ranjan in forum New To Java
    Replies: 7
    Last Post: 06-18-2009, 04:47 PM
  3. Replies: 0
    Last Post: 04-07-2009, 10:35 PM
  4. Replies: 0
    Last Post: 03-03-2009, 08:46 AM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM

Posting Permissions

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