-1
int main(array<System::String ^> ^args)

{

    int arr[]={1,2,3,2,9,8,1,2,3,9};
    int a[9];
    int size = sizeof(arr)/sizeof(arr[0]);
    int str[256]= {'\0'};
    int i = 0;
    for(i ; i < size ; i++)
    {
        if(str[arr[i]] == 0 )
        str[arr[i]]= 1;
    }
    for( i = 0 ; i < size ; i++)
    {
        if( str[arr[i]] == 1)
        {
            a[i] = arr[i];
        }
    }
    for(i=0 ; i < size ; i++)
    {
        printf("%d->",a[i]);

    }


    return 0;
}

still in the new array a,I am getting old data...not sure wats missing here...

Any help would be highly appreciated.

Thanks in advance.

P0W
  • 46,614
  • 9
  • 72
  • 119
Anand
  • 621
  • 3
  • 9
  • 31
  • possible duplicate of [Algorithm: efficient way to remove duplicate integers from an array](http://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array) – ThisaruG Dec 20 '14 at 09:47
  • i have seen this question,but not sure wats wrong with my code here... – Anand Dec 20 '14 at 09:48
  • 1
    You're not initializing `a`. Also, that's not C. Looks like C++-CLI. If you want to program in C, make sure you use a C compiler. – Mat Dec 20 '14 at 09:49
  • m doing in visual studio...even after initialising i m getting same result – Anand Dec 20 '14 at 09:50

1 Answers1

4

Logical mistakes

  • the final array won't go till size
  • the indexing for a is wrong

Simply use one loop to achieve this, analyze following :

int i = 0,j=0;

for( ; i < size ; i++)
{
    if(str[arr[i]] == 0 )
    { 
        str[arr[i]]= 1;
        a[j++] = arr[i];
    }
}

Now iterate till j on final array a

P0W
  • 46,614
  • 9
  • 72
  • 119