Saturday, 30 January 2016

Delete Duplicate Elements in Array Problem Solution Using C++.


Solution:

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int max = -999999,result[1000],ar[] = {-1,-33,-2,9,1,11,2,5,4,7,8,9,2,3,1,4,7,5,2,3,6,9,0,4,5,1,1,4,0,10};
 int n = sizeof(ar)/sizeof(int);
 bool checkPos[1000],checkNeg[1000];
 memset(result,-1,sizeof(ar));
 memset(checkPos,false,sizeof(checkPos));
 memset(checkNeg,false,sizeof(checkNeg));
 
 for(int i = 0; i < n; i++){
  if(ar[i] < 0){
   checkNeg[ar[i]*-1] = true;
   result[ar[i]*-1]++;
  }
  else {
   result[ar[i]]++;
   checkPos[ar[i]] = true;
  }
  if(max < abs(ar[i]))max = abs(ar[i]);
 }
 for(int i = 0; i <= max; i++){
  if(checkPos[i] == true && result[i]!=-1){
   checkPos[i] = false;
   cout << i << " ";
  }
  if(checkNeg[i] == true && result[i]!=-1){
   checkNeg[i] = false;
   cout << -i <<  " ";
  }
 }
 cout << endl;
 return 0;
}

No comments:

Post a Comment