Thread: count/display non-repeated element of array

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    count/display non-repeated element of array

    hi guys
    I'm working of self-study project that needs to count how many an element of an array is repeated. And print out result.
    I've been asking myself whether there is a smarter way doing this. Can you please help?

    Code:
    For instance:
    input 5 5 5 6 7 8 91 1
    output:
    1       1     
    5       3
    6       1
    7       1
    8       1
    91     1
    number 5 appears 3 times, while other only shows up 1 time.

    My algorithm goes like this: Since i don't study to use dynamic array yet. I assume the max of number of input is 100, or inputNumberArray[100]={0}. Then if the number of is input is less than 100, i will only concentrate on the non-zero part of the array.
    There will be another array of coutingArray[100]={0} that will be used to store the counter's value for each element.

    First, sort the array in acesnding order
    so that: 1 5 5 5 6 7 8 91

    Then, (the tricky part), try to detect the repeating of each element.
    Code:
    //sourceArray=array of input numebrs
    //targetArray=array of counters
    void processArrays(int sourceArray[], int element, int targetArray[])
    {
    	for (int i=1; i<element;i++)
    	{
    		if (compareEl(sourceArray, i))
    		{
    			targetArray[i]=targetArray[i]+1;
    			cout << "\n TRUE " << targetArray[i];
    		} else
    		{
    			cout << "\n FALSE ";
    			continue;
    		}
    	}
    }
    And funtion compareEl to computer each element its LEFT neighbor and RIGHT neighbor.
    1. if LEFT is the same && RIGHT is the same, do not count
    2. if LEFT is different && RIGHT is different, do count
    3. if LEFT is the same && RIGHT is different, do not count
    4. if LEFT is different && RIGHT is the same, do count

    thanks so mcuh!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just to clarify the problem, is the following input valid, and what output would you expect from a working program?
    Code:
    91 5 3 5 5 6 7 8 91 1

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    sorry for the confusing!!
    the problem is to read a set of positive integer and output how many time a particular number appears. Assume that the maximum you can enter is 100 numbers.
    For example
    input : 5 5 5 4 3 2 8 9 3
    output
    Code:
    number        count
    2                   2
    3                   2
    4                   1
    5                   3
    9                   1
    thanks!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Consider
    countingArray[ sourceArray[i] ]++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    43
    Thanks!! I had tried that.
    But it has 1 fall back.

    Code:
    countingArraySize=20
    sourceArraySize=20
    and if, somehow, we randomly input 4 5 4 3 2 32
    then countingArray[ sourceArray[i] ]++ will be out of bound at element of "32"

    thanks for any info!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there a limit to how big the input numbers can be (e.g. only numbers from 1 - 100 can be entered)? If so, then make the size of the counting array that big.

    If there is no limit, or the limit is really big, then you'll probably want to use something other than a counting array. I'd use a map, but I don't know if you know that or are allowed to use it.

    Regardless, I don't think your original attempt will work because you are only looking at neighbors, but duplicate numbers don't necessarily have to be next to each other.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    43

    that's why I have to sort the input array first.
    say you input: 3 1 3 35 4 5 3 34
    after sorting: 1 3 3 3 4 5 34 35
    Then I have do the check neighbor correct?
    Is there any way smarter than that?

    Yes. I'm not reaching dynamic array yet. So, that's the limit then, input values have be smaller than the array size.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, sorting first sounds like a good solution if you want to keep your counting array. Do you know how to sort?

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    43
    I used bubble sort as it's the only I know so far.
    I counted frequency of elements of 1 array by using another array for counter.
    is there any way different than that?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't think you need the second array if you are sorting, do you? The counter array still has the problem of having to be big enough to hold all possible values.

    If you just output the counts as you go then you don't need the counter array, which will work because you are sorting first.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    43
    The reason i do the sort because I want to generate the ouput like this:
    INPUT: 5 5 5 4 3 2 8 9 3

    Code:
    number        count
    2                   2
    3                   2
    4                   1
    5                   3
    9                   1
    I have to output the each element ONCE.
    any smarter idea then mine?

    thanks!!

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Right. Sorting first is good. You don't need a separate array for the count and it will be output in order. Are you having a problem?

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    43
    You see the output?
    How can I output the array's elements. Each element should be only read ONCE?
    That's why I need the "check neighbor" function so that will ignore the repeated elements and only output the first one.

    INPUT: 5 5 5 4 3 2 8 9 3
    5 appears 3 times, the "check neighbor" will ignore the other 2 and output only the first 5.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The description how there are 4 special cases for counting (comparing LEFT and RIGHT) makes it sound your solution is a bit too complicated (and as I understand doesn't work).

    How hard would be to simply count items from a given position in an array until they are equal, then start counting from the position where a different number appeared?

    With pointers the outer loop might look like this:
    Code:
        while (it != end) {
            unsigned count = count_equal_neighbours(it, end);
            std::cout << *it << '\t' << count << '\n';
            it += count;
        }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> That's why I need the "check neighbor" function so that will ignore the repeated elements and only output the first one.

    Your original check neighbor function was too complicated. anon hinted at the solution, but once you've sorted the numbers, then a simple check neighbor algorithm would work. I don't even think you need a separate function. Just a loop, a counter, and a variable to hold the current value you're counting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM