code.cpp 663 B

123456789101112131415161718
  1. /* Create a function that takes an array of items, removes all
  2. * duplicate items and returns a new array in the same sequential
  3. * order as the old array (minus duplicates).
  4. */
  5. #include <set> // to use std::set
  6. #include <utility> // to use std::pair
  7. std::vector<std::string> removeDups(std::vector<std::string> arr) {
  8. std::vector<std::string> newArr;
  9. std::set<std::string> unique;
  10. for(int i=0;arr.size()>i;i++){
  11. // insert will succeed if string is unique
  12. std::pair<std::set<std::string>::iterator,bool>
  13. r = unique.insert(arr[i]);
  14. // unless insertion fails, add the string to newArr
  15. if (r.second) newArr.push_back(arr[i]);
  16. }
  17. return newArr;
  18. }