There is an array having duplicates i,e any number can be repeated any no of times .write a programme to print for each no how many times it is repeated.EX:1,1,1,1,1,2,3,2,3,11 is repeated 6 times2 is repeated 2 times3 is repeated 2 times

Showing Answers 1 - 10 of 10 Answers

umeshknair

  • Nov 2nd, 2006
 

Hi trY this
One thinG keep in mind that u must pass a sorted arraY to this function

void duplicate(int *a, int n)
{
    int i, k;
    int *count, *temp;

    temp=a;
    for (i = 0; i < n; i++) count[i]=1;

    k = 0;
    for (i = 1; i < n; i++)
       if (temp[k] == temp[i]) count[k]=count[k]+1;
       else temp[++k] = temp[i];

    for (i = 0; i <= k; i++)
       printf("%d is repeated %d time(s)n", temp[i], count[i]);
}

-dot-

  Was this answer useful?  Yes

Sharath Ballal

  • Nov 23rd, 2006
 

I have written a program that will create a linked list to store the array element and its count and later print the same.  I have ended the array with a NULL so '0' cannot be counted. It can be worked around by passing the array size.

I use this approach instead of sorting the array and printing the same as sorting might take huge amount of time for array of very large length.

#include <stdio.h>
typedef struct node *Node;
Node count(int x[]);
void printANDfree(Node list);

struct node{
  int num;
  int count;
  Node next;
};

main()
{
    int x[]={5,1,1,6,1,5,1,6,1,2,3,2,3,1,''};
    Node list;

    list=count(x);
    printANDfree(list);
}

Node count(int x[]){
    int i;
    Node head=NULL, temp=NULL, curr=NULL;
    head=(Node)malloc(sizeof(struct node));
    head->num=x[0];
    head->count=1;
    head->next=NULL;

    for(i=1;(x[i]!='');i++){
      if(x[i]==head->num){
        head->count++;
        continue;
      }else if(x[i]<head->num){
        temp=(Node)malloc(sizeof(struct node));
        temp->num=x[i];
        temp->count=1;
        temp->next=head;
        head=temp;
        continue;
      }/*end else if*/
      curr=head;
      while(curr->next && (x[i]!=curr->next->num)&&(x[i]>curr->next->num)){
        curr=curr->next;
      }
      if(x[i]==curr->next->num){
        curr->next->count++;
      }
      else {
        temp=(Node)malloc(sizeof(struct node));
        temp->num=x[i];
        temp->count=1;
        temp->next=curr->next;
        curr->next=temp;
      }/*end else*/
    } /*end for*/
    return head;
}/*end count*/

void printANDfree(Node list){
  Node temp;

  while(list){
    printf("%d is repeated %d timesn",list->num, list->count);
    temp=list;
    list=list->next;
    free(temp);
  }/*end while*/
}/*end printANDfree */

  Was this answer useful?  Yes

Sharath Ballal

  • Nov 23rd, 2006
 

By the way when I ran the program above given by umesh on an Unix m/c it core dumped.

  Was this answer useful?  Yes

Manoj

  • Dec 27th, 2006
 

Yes, that will crash since there is no allocation of memory for two pointers. I wrote a simple O(n^2) program which may not be the best but works ! #include int main(){ int a[] = {1,1,1,1,1,2,3,2,3,1}; int len = sizeof(a)/sizeof(a[0]); int b[len][2]; //[num][count] int i,j,k; for(i=0;i

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions