Reputation: 21
I have 2 arrays. Input 1 and Input 2. Suppose input 1 has {1,1,2,3,3,4,5} and input 2 has {4,2,6,7,8}. Merged array {1,1,2,3,3,4,5,4,2,6,7,8}
After sorting my merged array looks like {1,1,2,2,3,3,4,4,5,6,7,8}
My output should be like {5,6,7,8} because those are non-repeating elements. It should have neither occurred twice in 1st nor once in 1st and once in 2nd.
I have put both the arrays into one single array(merged both). Sorted it and removed duplicates also. I am getting the output like {1,2,3,4,5,6,7,8} but i should not get 1,2,3,4 as they all have occurred twice in the merged array.
Please help me to finish the program soon. Thanks in advance.
I cant use structures. Thats my task.
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[10],c[10],i,j,n,n1,temp;
clrscr();
printf("enter the no of ele in array1\n");
scanf("%d",&n);
printf("enter 1st array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter size of 2nd array");
scanf("%d",&n1);
printf("enter 2nd array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
c[i]=a[i];
}
j=0;
for(i=n;i<n+n1;i++)
{
c[i]=b[j];
// printf("\n2nd array values are %d",c[i]);
j++;
}
for(i=0;i<n+n1;i++)
{
printf("%d\n",c[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(c[j]>c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
for(j=0;j<n+n1;j++)
{
printf("sorted is %d\n",c[j]);
}
for(i=0;i<n+n1;i++)
{
if(c[i]==c[i+1])
{
temp=c[i];
// printf("%d r d rep.ele\n",c[i]);
}
else
printf("%d r d ele\n",c[i]);
}
getch();
}
Upvotes: 1
Views: 15427
Reputation: 1
#include<stdio.h>
int main()
{
int a[30],b[30];
int n,k,i,j,p,c=0;
printf("How many element in first array\n");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); //FIRST ARRAY INPUT
}
printf("How many element in second array\n");
scanf("%d",&k);
printf("Enter elements");
for(i=0;i<k;i++)
{
scanf("%d",&b[i]); // SECOND ARRAY INPUT
}
printf("The new array is \n");
for(i=n,j=0;i<(n+k);i++)
{
a[i]=b[j++]; // MERGE ARRAY B[30],IN A[30] BY EXTEND IT.
}
for(i=0;i<(n+k);i++)
{
printf("%d ",a[i]); // PRINT THE MERGED ARRAY
}
for(i=0,p=0;i<(n+k);i++) // ELIMINATE DUPLICATE ELEMENTS
{
if(a[i]!=NULL)
{
for(j=i+1;j<(n+k);j++)
{
if(a[i]==a[j]) // IF ANY ELEMENTS MATCH TO NEXT OTHERS ARRAY ELEMENTS THEN, THOSE ELEMENTS ARE ASSIGNED BY NULL.
{
a[j]=NULL;
}
}
a[p++]=a[i];
c++; // COUNTING FOR SIZE OF NEW ARRAY
}
}
printf("\nAfter eliminate the duplicate elements,the new array is : \n");
for(i=0;i<c;i++)
{
printf("%d ",a[i]); // PRINT NEW ARRAY
}
return 0;
}
Upvotes: 0
Reputation: 215
Let us solve this in an inappropriate, yet funny, way:
#include <stdio.h>
#define MAXINPUT 10
void pack(const int* k, int n, int* table)
{
for(int i = 0; i < n; ++i)
{
table[k[i]]++;
}
}
void dump(const int* table)
{
int flag = 0;
for(int i = 0; i < MAXINPUT; ++i)
{
if(table[i] == 1)
{
printf("%s%d", (flag) ? ", " : "", i);
flag = 1;
}
}
}
int c[MAXINPUT];
int main(void)
{
const int a[] = { 1, 1, 2, 3, 3, 4, 5};
const int b[] = { 4, 2, 6, 7, 8};
pack(&a[0], sizeof(a)/sizeof(int), &c[0]);
pack(&b[0], sizeof(b)/sizeof(int), &c[0]);
dump(&c[0]);
return 0;
}
Upvotes: 1
Reputation: 40145
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0)
#define numcmp(type) int cmp_##type(const void *a, const void *b){\
type x = *(type*)a;\
type y = *(type*)b;\
return NUMCMP(x,y);\
}
numcmp(int);//define function cmp_int
void print_array(int a[], size_t size){
size_t i;
printf("{");
for(i=0;i<size;++i){
printf("%d", a[i]);
if(i != size-1)
printf(",");
}
printf("}\n");
}
int main(void){
int input1[] = {1,1,2,3,3,4,5};
int input2[] = {4,2,6,7,8};
size_t input1_size = sizeof(input1)/sizeof(*input1);
size_t input2_size = sizeof(input2)/sizeof(*input2);
printf("input1: ");
print_array(input1, input1_size);
printf("input2: ");
print_array(input2, input2_size);
size_t marge_array_size = input1_size + input2_size;
int marge_array[marge_array_size];
memcpy(marge_array, input1, input1_size*sizeof(*input1));
memcpy(&marge_array[input1_size], input2, input2_size*sizeof(*input2));
printf("marge array: ");
print_array(marge_array, marge_array_size);
qsort(marge_array, marge_array_size, sizeof(*marge_array), cmp_int);
printf("marge array(after sort): ");
print_array(marge_array, marge_array_size);
size_t singles_size = marge_array_size;
int *singles = malloc(singles_size * sizeof(*singles));
size_t i;
int sp;
bool drop = false;
for(sp=-1, i=0;i<marge_array_size;++i){
if(!drop){
if(sp==-1){
singles[++sp] = marge_array[i];
continue;
}
if(singles[sp] == marge_array[i]){
--sp;
drop = true;
continue;
}
} else if(singles[sp+1] == marge_array[i])
continue;
drop = false;
singles[++sp] = marge_array[i];
}
if(sp+1 != singles_size){
singles_size = sp+1;
singles = realloc(singles, singles_size*sizeof(*singles));
}
printf("marge array(single num): ");
print_array(singles, singles_size);
size_t uniques_size = marge_array_size;
int *uniques = malloc(uniques_size * sizeof(*uniques));
for(sp=-1, i=0;i<marge_array_size;++i){
if(sp==-1){
uniques[++sp] = marge_array[i];
continue;
}
if(uniques[sp] == marge_array[i]){
continue;
}
uniques[++sp] = marge_array[i];
}
if(sp+1 != uniques_size){
uniques_size = sp+1;
uniques = realloc(uniques, uniques_size*sizeof(*uniques));
}
printf("marge array(uniques num): ");
print_array(uniques, uniques_size);
//be free uniques and singles
return 0;
}
Upvotes: 0
Reputation: 8961
If you already sorted and merged the Arrays it's pretty simple: Just compare to the predecessor & successor. But some extra logic is needed to not run out of bounds.
#include <stdio.h>
int main(int argc, char** argv){
const int sortedMergedArray[] = {1,1,2,2,3,3,4,4,5,6,7,8};
const size_t mergedArrayLength = 12;
for(size_t i=0; i<mergedArrayLength;i++){
int predecessor = -1;
int successor = -1;
if(i >= 1) predecessor = sortedMergedArray[i-1];
if(i <= mergedArrayLength-1) successor = sortedMergedArray[i+1];
if(sortedMergedArray[i] != successor &&
sortedMergedArray[i] != predecessor){
printf("%d, ", sortedMergedArray[i]);
}
}
return 0;
}
Outputs:
5, 6, 7, 8,
Upvotes: 0
Reputation: 538
You're almost there! Don't try to be fancy, just create a struct with two int fields: value and count. Create an array of these structs and iterate through your sorted list. In the end you iterate through your array of structs and select all the values where the corresponding count is 1.
There are some details to sort out, like how large should your array of structs be, but hey, you're obviously not afraid of a bit of coding. If the array is bigger than you need, well... who cares as long as it's big enough?
Upvotes: 0
Reputation: 22542
size_t i, non_repeating_index = 0;
// allocate memory to hold the new array
int * non_repeating_elements = calloc(sizeof(int), merged_array_length);
// check if the first element is unique
if (merged_array_length >= 2 && merged_array[0] != merged_array[1])
non_repeating_elements[non_repeating_index++] = merged_array[0];
// for all other elemnts check if it equals the ones around it
for (i = 1; i < merged_array_length -1; i++) {
if (merged_array[i-1] != merged_array[i] && merged_array[i] != merged_array[i + 1])
non_repeating_elements[non_repeating_index++] = merged_array[i];
}
// check the last element
if (merged_array_length >= 3 && merged_array[i-1] != merged_array[i])
non_repeating_elements[non_repeating_index++] = merged_array[i];
// resize the array to conserve memory
non_repeating_elements = realloc(non_repeating_elements, sizeof(*non_repeating_elements) * non_repeating_index);
Upvotes: 0