Counting distinct sensor IDs in place

A vector named ids holds the integer sensor IDs logged during a test run, with many repeats in no particular order. You want to remove the duplicates in place using standard algorithms and then report how many distinct sensors reported. Arrange the steps in the order they must run.

  1. 1.

    Read ids.size() as the number of distinct sensor IDs.

  2. 2.

    Call ids.erase(newEnd, ids.end()) to remove the leftover elements after the new logical end.

  3. 3.

    Call auto newEnd = std::unique(ids.begin(), ids.end()) and keep the iterator it returns.

  4. 4.

    Call std::sort(ids.begin(), ids.end()) so that equal IDs sit next to each other.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More C++ Standard Library Questions: