C++ (Qt)vector<int> a; a.push_back(1); a.push_back(5); a.push_back(3); a.push_back(8); a.push_back(4); a.push_back(3); a.push_back(8); a.push_back(5); a.push_back(2); int n = a.size(); for (int i=0;i<n;++i){ if(a[i]==i+1 || a[i]>n) continue; //i.e. either inplace or duplicate else{ while(true){ if(a[i]>n) //i.e. a duplicate that is already detected break; if (a[a[i]-1]==a[i]) { a[i]+=n; //i.e. a duplicate is detected break; } swap(&a[i],&a[a[i]-1]); } } } vector<int> out; for (int i=0;i<n;++i){ if(a[i]<n) out.push_back(a[i]); }
Cint removeDuplicateAndSort_ON_O1 (int* arr, int count) { int n, i; for(i=0; i<count; ++i) { // check error values if(arr[i] < 1) {return -1;} if(arr[i] > count) {return -1;} } for(i=0; i<count;) { n = arr[i]-1; if(n == -1) {++i; continue;} // "Removed Value" if(n == i) {++i; continue;} // true place if(n == arr[n]-1) { arr[i] = 0; // mark as "Removed Value" ++i; } else { arr[i] = arr[n]; // swap arr[n] = n+1; } } n = 0; for(i=0; i<count; ++i) { // remove "Removed Value" if(!arr[i]) {continue;} arr[n] = arr[i]; ++n; } return n;}
C++ (Qt)void MarkDupAsNeg( int * arr, int n ){ for (int i = 0; i < n - 1; ++i) { int match = arr[i]; if (match < 0) continue; for (int j = i + 1; j < n; ++j) if (arr[j] == match) arr[i] = arr[j] = -1; } } int RemoveNeg( int * arr, int n ){ int i, place = 0; for (i = 0; i < n; ++i) { if (arr[i] < 0) continue; if (i != place) arr[place] = arr[i]; ++place; } return place;}
C++ (Qt) ...