Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Using the count element method to count the occurrence of characters in an array

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Using the count element method to count the occurrence of characters in an array

    Hello guys,

    I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}

    now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'}
    Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.

    How would one go about this using the counting elements technique?

    I tried this but not sure how to use the counting elements technique.

    char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'};
    char[] bucket = new char[(arr[0] * arr.length)];
    for (int i = 0; i < len; i++)
    bucket[arr[i]]++;


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    the counting elements technique
    Can you describe how that technique works?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    I honestly don't know how it works except it a technique used to count the number of occurrence of numbers.. so given an array with numbers{0,1,2,1,3,3,2,1,1}
    the counting technique will work thus:
    count[0] == 0(since the initial array had only 1 occurrence of 0)
    count[1] == 4(number 1 occured 4 times in the original array)
    count[2] ==2(occurred twice)
    count[3] ==3(occured 3)

    in the end, the length of the count is equal to the length of the number of the unique numbers ignoring how many times they occur.

    I don't know if this could be used for characters instead of numbers, which is what I am trying to replicate using the counting element technique.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    It looks like with single digit numbers, the numeric value is used as the array index.
    With letters, what characters/letters in the alphabet are supposed to be counted? a to z ignoring case?
    If you work with char values: the index could be computed by subtracting 'a' from it.
    For example if the char is 'b', 'b' - 'a' = 1 for the index ('a' would have an index of 0)

    Are your counts right?
    There is one 0, not zero? And there are two 3 not three.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Hi,

    so, could you write a small java code to use the counting element technique on numbers? I could then try to modify it for the alphabets. The problem I am having is how to successfully initialize the bucket array. In my code given below :

    int [] arr = new int {1,2,3,3,3,2,1,2};
    int[] bucket = new int[(arr[0] * arr.length)];
    for (int i = 0; i < len; i++)
    bucket[arr[i]]++;
    When I tested this code framgment, It breaks because, I might have done the bucket array initialization wrongly. If you could help with how to properly initialize the bucket, I think I can handle the rest.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Using an array 'bucket' to count the frequency of occurrence of certain items requires the array to be the same size as the number of unique items being counted. For example, if the items being counted are the digits 0 - 9, then the size of the frequency array would be 10. If the items being counted are lowercase characters, then the size of the array would be 26.

    If you want to size the 'bucket' to the be the same as the number of unique items in the range of items being counted, then you'll first need to determine the number of unique items. For the first example you gave, the size would be 8 for the unique characters 'E', 'L', 'P', 'H', 'A', 'N', 'T', 'O'. Your first task may be to determine the number of unique items, and a Set can be used to accomplish that.

    Another approach is to size the array 'bucket' to be the largest needed (26 again for capital letters) and only report the resulting elements with values greater than zero. This approach accomplishes 2 things: determines the number of unique items AND the frequency of each of the items.

    The thinking behind your sizing is not clear. Perhaps you could explain:

    int[] bucket = new int[(arr[0] * arr.length)];

  7. #7
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Hi,

    I have tried some of the suggestions you gave but unfortunately, when I debug my code, I don't see anything happening.

    int[] bucket = new int[9] in the case of an array with 8 elements.

    then when i tried this :
    for i->8
    bucket[arr[i]]++;
    end for loop
    this should simply be adding up all the numbers in the array. so, if i = 0, it stores 0 in index 0 and 1 in index 1 and just adds the number of times it encouters 1. for an array having characters, I think this might be the way to go about it:

    for i-->8
    char s = arr.charAt(i);
    int i = s-'a'; //convert the character to a digit
    bucket[arr[i]]++;
    end for loop
    I don't know if this is correct way to try this out with a character array using the counting elements. My most worrying concern now is even when I try the counting element with numbers, I still don't get the right results.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    I still don't get the right results.
    Can you post the code and its results so we can see what it does?

    Please wrap the code in code tags, not quote tags.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Hi,
    Ok, using information from you and the other friends on the thread, I have been able to get so so close to my goal. The code below is used to convert each character into an integer that is now stored in the bucket .

    int len = arr.length;
    int[] bucket = new int[26+1];
    for(int i = 0; i<26;++i){
    if(i<arr.length)
    bucket[arr[i]-'a']++;
    }
    This gives me the following value in the bucket:

    [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]
    The respective array I used had the value ['k','i','s', 's','a'].
    so, 1 a, 1 k, 1 i and 2 s. The next thing I want to get now is how to convert the digit array back to characters. Any help on this?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Please wrap the code in code tags, not quote tags.


    how to convert the digit array back to characters.
    char variables can be used in math expressions like ints:
    'a' + 2 = 'c'

    To print an int as a char using casting: (char)49 = '1'
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Using the count element method to count the occurrence of characters in an array

    Thanks alot. I am grateful.

Similar Threads

  1. Count occurrence of a string in an array list
    By Parth Raval in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2014, 12:04 AM
  2. The count for each value in array
    By samar in forum Loops & Control Statements
    Replies: 4
    Last Post: January 4th, 2013, 02:38 PM
  3. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  4. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 02:50 AM
  5. Method to count objects
    By mjpam in forum Java Theory & Questions
    Replies: 5
    Last Post: July 28th, 2010, 09:49 AM

Tags for this Thread