Guest User

Untitled

a guest
Mar 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. int removeDuplicatedValues(string array[], int size)
  2. {
  3. int newsize = size;
  4. string str;
  5. for(int i = 0; i < size; i++)
  6. {
  7. str = array[i];
  8. // note that we increment until j < newsize here because
  9. // we are "shrinking" the array on every match!
  10. for(int j = i + 1; j < newsize; j++)
  11. {
  12. if(array[j] == str) ///< we found a match
  13. {
  14. for(int k = j; k < size - 1; k++) ///< size - 1 for "removal"
  15. {
  16. array[k] = array[k + 1]; ///< shift everybody else down
  17. }
  18. newsize--; ///< update the size after we "removed"
  19. }
  20. }
  21. }
  22. return newsize;
  23. }
  24.  
  25. int main()
  26. {
  27. string duplicates[7] = { "apple", "bear", "apple", "daddy", "bear", "daddy", "sunny" };
  28. int length = removeDuplicatedValues(duplicates, 7);
  29. for (int i = 0; i < length; i++)
  30. {
  31. std::cout << duplicates[i] << " ";
  32. }
  33. std::cout << std::endl;
  34. return 0;
  35. }
  36.  
  37. apple bear daddy sunny
  38.  
  39. int duplicates = 7 - length; ///< how many duplicates did we have?
  40.  
  41. #include <iostream>
  42. #include <string.h>
  43. using namespace std;
  44. string* arrayNew;
  45. int arrayNewSize=0;
  46. string *removeDuplicatedValues(string *array, int arraySize)
  47. {
  48. int duplicatecounter = 0;
  49. string empty = "";
  50. for (int i = 0; i < arraySize; i++)
  51. {
  52. string s = array[i];
  53. for (int j = i + 1; j < arraySize; j++){
  54. if (array[j] == s)
  55. {
  56. array[j] = empty;
  57. }
  58. }
  59. if(array[i]!=empty){
  60. arrayNewSize++;
  61. }
  62. }
  63. arrayNew = new string[arrayNewSize];
  64. int k=0;
  65. for (int j = 0; j < arraySize; j++){
  66. if(array[j]!=empty){
  67. arrayNew[k++]=array[j];
  68. }
  69. }
  70. return arrayNew;
  71.  
  72. }
  73. int main () {
  74. string duplicates[] = {"apple","bear","apple","daddy","bear","daddy","sunny"};
  75. int arraySize = (sizeof(duplicates) / sizeof(string));
  76. string* finalArrayP;
  77. finalArrayP=removeDuplicatedValues(duplicates,arraySize);
  78. for (int i = 0; i < arrayNewSize; i++){
  79. cout << "newarray[i] is " << finalArrayP[i] << endl;
  80. }
  81. delete [] arrayNew;
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment