Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int removeDuplicatedValues(string array[], int size)
- {
- int newsize = size;
- string str;
- for(int i = 0; i < size; i++)
- {
- str = array[i];
- // note that we increment until j < newsize here because
- // we are "shrinking" the array on every match!
- for(int j = i + 1; j < newsize; j++)
- {
- if(array[j] == str) ///< we found a match
- {
- for(int k = j; k < size - 1; k++) ///< size - 1 for "removal"
- {
- array[k] = array[k + 1]; ///< shift everybody else down
- }
- newsize--; ///< update the size after we "removed"
- }
- }
- }
- return newsize;
- }
- int main()
- {
- string duplicates[7] = { "apple", "bear", "apple", "daddy", "bear", "daddy", "sunny" };
- int length = removeDuplicatedValues(duplicates, 7);
- for (int i = 0; i < length; i++)
- {
- std::cout << duplicates[i] << " ";
- }
- std::cout << std::endl;
- return 0;
- }
- apple bear daddy sunny
- int duplicates = 7 - length; ///< how many duplicates did we have?
- #include <iostream>
- #include <string.h>
- using namespace std;
- string* arrayNew;
- int arrayNewSize=0;
- string *removeDuplicatedValues(string *array, int arraySize)
- {
- int duplicatecounter = 0;
- string empty = "";
- for (int i = 0; i < arraySize; i++)
- {
- string s = array[i];
- for (int j = i + 1; j < arraySize; j++){
- if (array[j] == s)
- {
- array[j] = empty;
- }
- }
- if(array[i]!=empty){
- arrayNewSize++;
- }
- }
- arrayNew = new string[arrayNewSize];
- int k=0;
- for (int j = 0; j < arraySize; j++){
- if(array[j]!=empty){
- arrayNew[k++]=array[j];
- }
- }
- return arrayNew;
- }
- int main () {
- string duplicates[] = {"apple","bear","apple","daddy","bear","daddy","sunny"};
- int arraySize = (sizeof(duplicates) / sizeof(string));
- string* finalArrayP;
- finalArrayP=removeDuplicatedValues(duplicates,arraySize);
- for (int i = 0; i < arrayNewSize; i++){
- cout << "newarray[i] is " << finalArrayP[i] << endl;
- }
- delete [] arrayNew;
- return 0;
- }
Add Comment
Please, Sign In to add comment