0

I'm trying to write a recursive function to find duplicates in an array of integers. For example if the array is: {4, 1, 4, 3, 2, 3} it should returns 2. I tried a mergesort-like approach but without success. Someone can help?

My try (works only with ordered arrays):

int count(int arr[], int bot, int top){
  if(bot==top) return 0;
  else{
    int med = (bot+top)/2;
    int n = count(arr, bot, med) + count(arr, med+1, top);
    if(arr[med]==arr[med+1]) n++;
    return n;
  }
}
6
  • Welcome on stack overflow, please consider posting a question about a specific programmation issue using a SSCCE. Thank you. Commented Jun 12, 2015 at 8:18
  • @YuHao I didn't put cause i don't think could be improved. I think it's needed a different algorithm. Anyway i've just updated the post. Commented Jun 12, 2015 at 8:24
  • What exactly is the expected result? The maximum of the number of occurrences of each entry? Commented Jun 12, 2015 at 9:43
  • 1
    @Codor the number of duplicated entries in the array (other examples {3, 3, 3, 1} should return 1, {2,2,2,2} should return 1, {4,4,1,1,5,5,4} should return 3) Commented Jun 12, 2015 at 9:55
  • Apparently the desired result does not yield a divide and conquer approach; it is unclear how the results of the split instance are related. Commented Jun 12, 2015 at 11:46

1 Answer 1

1

You are just checking if arr[med]==arr[med+1] which will have a problem when you have a case like 111 then the count will become two but the count should actually be one. So add an extra flag to check if the same element is repeated or not.

Sort the array. May be you can use merge sort or something to do that and then something like this should work!

#include <stdio.h>

int main(void) {
    // your code goes here
    int a[16] = {1,1,1,1,1,1,1,1,1,1,1,2,2,3,3,5};
    int out = count(a,0,15);
    printf("%d\n",out);
    return 0;
}

int count(int arr[], int bot, int top){
  int flag = 0;
  if(bot==top) return 0;

  else{
    int med = (bot+top)/2;
    int n = count(arr, bot, med) + count(arr, med+1, top);
    if(arr[med]==arr[med+1])
    {
        flag = arr[med-1];
        if(flag != arr[med])
            n++;
    }
    return n;

    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

my goal is to do that without ordering first the array :(
@Brontolo Why not? Are you worried about performance? Is this homework? It's hard to help without some context.
@jacwah I'm sorry jacwah, you're right. Yes this is an exercise I found to do for my programming course. So I suppose the goal is to do just what is asked for!
@Brontolo I'm not sure that a recursive algorithm can be applied if the input can't first be sorted. Are you sure you read the exercise description right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.