task2.cpp 961 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <iostream>
  2. int* removeDuplicates(int* arr, int size, int& newSize) {
  3. int* uniqueArr = new int[size];
  4. int uniqueCount = 0;
  5. for (int i = 0; i < size; ++i) {
  6. bool isDuplicate = false;
  7. for (int j = 0; j < uniqueCount; ++j) {
  8. if (uniqueArr[j] == arr[i]) {
  9. isDuplicate = true;
  10. break;
  11. }
  12. }
  13. if (!isDuplicate) {
  14. uniqueArr[uniqueCount++] = arr[i];
  15. }
  16. }
  17. newSize = uniqueCount;
  18. return uniqueArr;
  19. }
  20. int main() {
  21. int arr[] = {1, 2, 2, 3, 4, 4, 4, 5};
  22. int size = sizeof(arr) / sizeof(arr[0]);
  23. int newSize;
  24. int* uniqueArr = removeDuplicates(arr, size, newSize);
  25. std::cout << "{";
  26. for (int i = 0; i < newSize; ++i) {
  27. std::cout << uniqueArr[i];
  28. if (i < newSize - 1) {
  29. std::cout << ", ";
  30. }
  31. }
  32. std::cout << "}" << std::endl;
  33. delete[] uniqueArr;
  34. return 0;
  35. }