# Thread: how to compare element of array with other element

1. Originally Posted by laserlight
Suppose you had input like this:
1 1 1 2 2 3 3 4 4 4 5
Now, if you want to count the input, it's quite easy: you start with the first number, and set count=1. Then you keep looping over the remaining numbers, incrementing the count until you find a number that doesn't match. So, you end up with count=3, then you encounter 2. Great. You print "1 appears 3 times", reset count=1, and now you loop over the remaining elements until you find an element not equal to 2. So on and so forth.

But what happens if you had input like this?
1 2 5 1 4 3 4 2 1 3 4
Clearly, you cannot use the same approach. What you could do is to use a variation of your original "check for a repeat" algorithm to count instead, but while that would work for the very first time you encounter each unique number, subsequently you'll end up printing nonsense. You could adjust your algorithm to always check the entire array instead of just the remaining elements, but then you'll end printing the same output multiple times.

So, one way to fix this is to sort the array, then apply the consecutive matching algorithm I outlined earlier. Another way to fix this is to track the numbers that have been counted, hence the second approach in which I talked about using a second array.

The reason why I wrote "don't worry about the counting yet" is that if you want to do the first approach, you need to be sure that you're sorting correctly first. If you try to implement the sort and count at one go, you'll probably fail, and you'll have a hard time figuring out why.

If you prefer, you can focus on the counting first, but then you must assume that your input is sorted. In that case, only when you have the counting done right do you do the sorting.
Ok so I am sorting array

Code:
```#include <stdio.h>
int main()
{
int i = 0; int j = 0;

int list[5] = {1, 2, 1, 3, 4};

int temp;

for (i = 0; i <5; i++)

{

for (j = i+1 ; j <5; j++)
{

if (list[i]>list[j]) // if the one is greater then other swap value
{
temp = list[i];
list[i] = list[j];
list[j] = temp;

}

}

printf("%d", list[i]);
}

return 0;
}```
11234

how to know how many time number repeats in list ?

2. Originally Posted by Djsarkar
Ok so I am sorting array

Code:
```#include <stdio.h>
int main()
{
int i = 0; int j = 0;

int list[5] = {1, 2, 1, 3, 4};

int temp;

for (i = 0; i <5; i++)

{

for (j = i+1 ; j <5; j++)
{

if (list[i]>list[j]) // if the one is greater then other swap value
{
temp = list[i];
list[i] = list[j];
list[j] = temp;

}

}

printf("%d", list[i]);
}

return 0;
}```
11234

how to know how many time number repeats in list ?
Code:
```Loop I = 0 to length
Set REPEATS counter to zero
Loop J = I + 1 to length
If data at J equals data at I
Increment REPEATS
Otherwise
Exit inner loop
If REPEATS is greater than zero
Report/store/whatever```

3. Originally Posted by Sir Galahad
Code:
```Loop I = 0 to length
Set REPEATS counter to zero
Loop J = I + 1 to length
If data at J equals data at I
Increment REPEATS
Otherwise
Exit inner loop
If REPEATS is greater than zero
Report/store/whatever```
There is a small but critical piece missing though: after counting with the inner loop, I must be set to J in order to skip those elements that have already been counted.

4. Originally Posted by laserlight
There is a small but critical piece missing though: after counting with the inner loop, I must be set to J in order to skip those elements that have already been counted.
Yep. Good catch.

5. Originally Posted by Sir Galahad
Code:
```Loop I = 0 to length
Set REPEATS counter to zero
Loop J = I + 1 to length
If data at J equals data at I
Increment REPEATS
Otherwise
Exit inner loop
If REPEATS is greater than zero
Report/store/whatever```
I guess method is for without sorting array

6. Instead of guessing, why don't you go through the algorithm with unsorted input and see what you get, and then implement it and try it for an unsorted array to confirm?

7. Originally Posted by laserlight
Instead of guessing, why don't you go through the algorithm with unsorted input and see what you get, and then implement it and try it for an unsorted array to confirm?
I have set one counter to count repeated number but it's not work as I want

Code:
```#include <stdio.h> int main()
{
int list [5] = {1, 2, 1, 2, 4};

int i = 0;  int j = 0; int counter = 0;

for (i=0; i<5; i++)
{
for ( j = i + 1; j < 5; j++)
{

if (list[i] == list[j])
{
printf(" Number %d is repeated in list \n", list[i]);

counter++;
}

}

}
printf("  %d \n", counter);
counter = 0;
return 0;
}```
Number 1 is repeated in list
1
Number 2 is repeated in list
2
2
2
2

8. That's because you didn't implement the algorithm correctly. Look carefully at what should be in which loop.

9. Originally Posted by laserlight
That's because you didn't implement the algorithm correctly. Look carefully at what should be in which loop.
I don't understand how your algorithm will implement
I am trying to fix flow chart

10. Okay, try it with sorted input. Does it work?

11. No the process shown in flowchart to find repeated number in list not to count number

12. Sorry, I don't understand what you're trying to say.

13. I don't understand the algorithm for non-sorted. I am having trouble counting the number that is repeated more than once. I tried so many times but still struggling

14. Originally Posted by Djsarkar
I don't understand the algorithm for non-sorted.
Do you understand the algorithm for counting repeats when the input is sorted (i.e., see posts #14, #17, and #18)?

The algorithm for non-sorted input is easy: just sort, then apply the algorithm for sorted input.