# count/display non-repeated element of array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-08-2008
azsquall
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.

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!!!
• 07-08-2008
Daved
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`
• 07-08-2008
azsquall
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!!
• 07-08-2008
Salem
Consider
countingArray[ sourceArray[i] ]++
• 07-09-2008
azsquall
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!
• 07-09-2008
Daved
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.
• 07-09-2008
azsquall
:)
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.
• 07-09-2008
Daved
Yes, sorting first sounds like a good solution if you want to keep your counting array. Do you know how to sort?
• 07-09-2008
azsquall
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?
• 07-09-2008
Daved
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.
• 07-09-2008
azsquall
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!!
• 07-09-2008
Daved
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?
• 07-10-2008
azsquall
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.
• 07-10-2008
anon
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;     }```
• 07-10-2008
Daved
>> 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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last