Find duplicates


Find duplicates
Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing any number of times.
#include <stdio.h>
#include <stdlib.h>
void printRepeating(int arr[], int size)                                                
{
  int i;
  printf("The repeating elements are: \n");
  for (i = 0; i < size; i++)
  {
    if (arr[abs(arr[i])] >= 0)
      arr[abs(arr[i])] = -arr[abs(arr[i])];
    else
      printf(" %d ", abs(arr[i]));
  }
}
int main()
{
  int arr[] = {1, 2, 3, 1, 3, 6, 6};
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  printRepeating(arr, arr_size);
  getchar();
  return 0;
}
Output:
The repeating elements are:
1  3  6

Find duplicates Find duplicates Reviewed by Unknown on August 26, 2018 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.