1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Counting elements in arrays: Visual Basic

Discussion in 'Software Development' started by jameshpg, Feb 14, 2007.

Thread Status:
Not open for further replies.
  1. jameshpg

    jameshpg Thread Starter

    Joined:
    Dec 26, 2006
    Messages:
    13
    Hi all,

    So i've got an array to hold names and I want to count how many times a name is held in the array. For example,

    John
    John
    James
    John
    James
    Jack
    Luke
    John
    Jack
    Luke

    If this was my array then I need a way to count how many times each name appears:
    John = 4
    James = 2
    Jack = 2
    Luke = 2

    Does anyone know of a way to do this? Its reallllyyyyy important!

    Regards,

    james
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    My VB knowledges are a bit too old and rusted, therefore I give you the algorithm
    in a comprehensive Java form :

    Code:
    [SIZE=2]
    // the array of String which contains the names,
    // I call it names
    
    String[] resultNames = new String[names.length]; // the array that will contain
                                                     // the distinct names
    int[] resultCounters = new int[names.length];    // the array that will contain
                                                     // the counter for each distinct name
    int numberDistinctNames = 0;                     // the number of distinct names
                                                     // that will be found initialized to 0
    int ptr = 0;                                    // the pointeur that will be used to
                                                     // browse the array of distinct names
    String priorItem = "";                           // the string that will be compared with
                                                     // each item of the array of names
    
    for (int i = 0; i < names.length; i++) {         // main loop in which each item of names
                                                     // will be read, the index of the first item is 0
    
        if (numberDistinctNames == 0) {              // check if the result arrays are empty 
            resultNames[ptr] = names[i];
            resultCounters[ptr] = 1;
            numberDistinctNames++;
            priorItem = names[i];
        } else {                                     // the result arrays are not empty
            if (priorItem.equals(names[i]) {         // check if the current read item is equal to the prior
                resultCounters[ptr]++;
            } else {
                boolean isFound = false;
                ptr = 0;
                while ((!isFound) && (ptr < numberDistinctNames)) { // browsing the array resultNames
                    if (names[i].equals(resultNames[ptr]) {         // to check if the current name has already
                        isFound = true;                             // been stored 
                    } else {
                        ptr++;
                    }
                }
                if (isFound) {
                   resultCounters[ptr]++;
                } else {
                   resultNames[numberDistinctNames] = names[i];
                   resultCounters[numberDistinctNames] = 1;
                   ptr=numberDistinctNames;
                   numberDistinctNames++;
                }
                priorItem = names[i];
            }
        }
    }
    
    // numberDistinctNames will contain the exact number of distinct
    // names 
    
    [/SIZE]
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/543985

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice