0

I need to write a program where I have to print the numbers which occur twice or more than twice in the array. To make things simpler, I am working on a sorted array.

Here is my code.

#include <stdio.h>
int main()
{
    int n,i;
    int a[10]={2,2,2,4,6,6,9,10,10,11};
    printf("%d\n",a[10]);
    for(i=0;i<10;++i)
    {
        if(a[i]==a[i+1] && a[i]!=a[i-1])
            printf("%d ",a[i]);
        else
            continue;
    }
    return 0;
}

The code seems to work fine but I do not like my code because at some point, the loop compares the value of a[0] with a[-1] and a[9] with a[10] and both of these, a[-1] and a[10], are garbage values. I am sure there are better ways to do it but I am unable to think of any.

Also, I need to extend the above program to count the frequency of duplicate numbers.

Help is appreciated. Thanks!

Pranav Arora
  • 993
  • 2
  • 10
  • 16

6 Answers6

2

First, you can't access a[10] in your printf line, this is outside your array.

This should work fine.

#include <stdio.h>
int main()
{
    int n,i;
    int a[10]={2,2,2,4,6,6,9,10,10,11};
    for(i=0; i < 9; ++i)
    {
        if(a[i] == a[i+1])
            if(i > 0 && a[i] == a[i-1])
                continue;
            else
                printf("%d ",a[i]);
    }
    return 0;
}

Edit: Or you can use the shorter yet harder to read:

for(i=0; i < 9; ++i)
{
    if(a[i] == a[i+1] && (!i || a[i] != a[i-1]))
        printf("%d ",a[i]);
}
  • Can you please help me with this problem too: http://stackoverflow.com/questions/25717820/unique-numbers-in-array ? – Pranav Arora Sep 08 '14 at 05:15
1

See code below for the solution, which will print only the duplicate numbers from the array and how many times they occur.

I added the int c which is used for your count. It is initially set to 1, and increased by 1 for each duplicate number.

#include <stdio.h>
using namespace std;

int main() {

  int n,i,c;
  int a[10]={2,2,2,4,6,6,9,10,10,11};
  c = 1;
  for(i=0; i < 9; ++i)
  {
    if (a[i] == a[i+1]) {
    n = a[i];
    c += 1;
    } else {
        if (c > 1) {
        printf("Number: %d, Occurences: %d \n",n,c);
        c = 1;
        }
    }
  }
  return 0;
}
Daniel Congrove
  • 3,519
  • 2
  • 35
  • 59
0

Since the array is already sorted, it'd be easier to do something like this, with another loop inside the for loop.

int duplicateCount = 1; // Must be at least one instance of a value in the array
for(int i = 0; i < 9; ++i) {
    int j = i;
    while(j != 9 && a[j] == a[j + 1]) {
        ++j;
        ++duplicateCount;
    }
    i = j; // j is now at end of list of duplicates, so put i at the end as well,
           // to be incremented to the next unique value at end of for loop iteration
    printf("%d: %d\n", a[j], duplicateCount);
    duplicateCount = 1; // Reset for next values
}

If you want to count the frequency of all the numbers, you can easily turn duplicateCount into an array of values to count the frequency of each unique value. For a better solution, you could use another data structure, such as a map.

jackarms
  • 1,343
  • 7
  • 14
0

You are really going to need to have two indexes that walk through your array. Here's a start:

int i = 0;
int j;

while (i < 10) {
    for (j = i+1; j < 10; ++j) if (a[i] != a[j]) break;
    if (j !+ i+i) printf("%d\n", a[i]);
    i = j;
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
0

You could use a functor, overload operator() and return a set (unique values). Assuming your array is sorted it's just a matter of comparing the previous one with the next and inserting it in the set if they equal. If they are not sorted then you have to go through the whole array for every entry. Below are two examples with output as an explanation.

#include <set>
using namespace std;

Unsorted array:

struct UnsortedArrayDuplicateEntries {
    set<int> operator()(int* array, int size) {
        set<int> duplicates;
        for (int i = 0; i < size - 1; i++)
            for (int j = i + 1;j < size; j++)
                if (array[i] == array[j]) 
                    duplicates.insert(array[j]);

        return duplicates;
    }
};

Sorted array:

struct SortedArrayDuplicateEntries {
    set<int> operator()(int* array, int size) {
        set<int> duplicates;
        for (int i = 0; i < size - 1; i++)
            if (array[i] == array[i+1]) 
                duplicates.insert(array[j]);

        return duplicates;
    }
};

Test sorted:

SortedArrayDuplicateEntries d;
int sorted[10]={2,2,2,4,6,6,9,10,10,11};
set<int> resultSorted = d(sorted,10);
for (int i : resultSorted) cout << i << endl;

Output sorted:

2
6
10

Test unsorted:

UnsortedArrayDuplicateEntries d;
int unsorted[10]={2,6,4,2,10,2,9,6,10,11};
set<int> resultUnsorted = d(unsorted,10);
for (int i : resultUnsorted) cout << i << endl;

Output unsorted:

2
6
10

I hope it helps!

fonZ
  • 2,428
  • 4
  • 21
  • 40
0
#include <stdio.h>

int main(){
    int i, a[10]={2,2,2,4,6,6,9,10,10,11};
    int size = 10;

    for(i=0;i<size;++i){
        int tmp = a[i];
        int c = 1;
        while(++i < size && tmp == a[i])
            ++c;
        if(c > 1)
            printf("%d times %d\n", tmp, c);
        --i;
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70