answersLogoWhite

0

The most efficient method is to sort the array and then remove duplicate neighbours. If you want to maintain the original order, you must do it the long way, comparing every element with every other element, removing duplicates as you go.

Both methods are shown below.

#include // std::cout and std::endl

#include // std::vector

#include // std::sort and std::unique

#include // pseudo-random numbers

#include // required to seed random numbers

template

unsigned remove_unsorted_duplicates (std::vector& arr)

{

// Maintain count of deletions.

unsigned deletions(0);

// Need at least 2 elements...

if (arr.size() < 2)

return deletions;

// Loop through all but the penultimate element (left elements).

unsigned left(0);

unsigned penultimate = arr.size() - 1;

while (left < penultimate)

{

// Loop through all elements to the right of left element (right elements).

unsigned right = left + 1;

while (right < arr.size())

{

// Duplicate?

if (arr[left] == arr[right])

{

// Left-shift all the elements from end of the array to fill the gap.

unsigned i = right;

while (i < penultimate)

{

arr[i] = arr[i+1];

++i;

}

// Remove the redundant element from the end of the array.

arr.pop_back ();

// Increment the deletion total.

++deletions;

// Decrement the penultimate count.

--penultimate;

}

else

{

// Not a duplicate -- move to next element.

++right;

}

}

// Move to next element from left.

++left;

}

// Return the number of deletions.

return deletions;

}

template

unsigned remove_sorted_duplicates (std::vector& arr)

{

unsigned size = arr.size();

std::sort (arr.begin(), arr.end());

std::vector::iterator it = std::unique (arr.begin(), arr.end());

arr.resize (std::distance (arr.begin(), it));

return size - arr.size();

}

template

void print_array (std::vector& arr)

{

for (unsigned i(0); i < arr.size(); ++i)

{

std::cout << arr[i] << ' ';

}

std::cout << std::endl;

}

int main()

{

// Pseudo-random number generator.

std::default_random_engine generator;

generator.seed ((unsigned) time (NULL));

std::uniform_int_distribution distribution (1, 5);

// Create an array with 10 random elements in the range 1 to 5.

// Guaranteed to create at least 5 duplicates.

std::vector arr (10);

for (unsigned i(0); i < arr.size(); ++i)

{

arr[i] = distribution (generator);

}

// Copy the array.

std::vector arr2 (arr);

std::cout << "Unsorted duplicate removal\n" << std::endl;

std::cout << "Before:\t";

print_array (arr);

unsigned deletions = remove_unsorted_duplicates (arr);

std::cout << "After:\t";

print_array (arr);

std::cout << "There were " << deletions << " deletions.\n" << std::endl;

std::cout << "Sorted duplicate removal\n" << std::endl;

std::cout << "Before:\t";

print_array (arr2);

deletions = remove_sorted_duplicates (arr2);

std::cout << "After:\t";

print_array (arr2);

std::cout << "There were " << deletions << " deletions.\n" << std::endl;

}

Example Output

Unsorted duplicate removal

Before: 2 2 1 3 1 3 1 4 3 2

After: 2 1 3 4

There were 6 deletions.

Sorted duplicate removal

Before: 2 2 1 3 1 3 1 4 3 2

After: 1 2 3 4

There were 6 deletions.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

C program to duplicate element from a set of elements?

Cannot be solved without knowing what an 'Element' is.


Where can I find wedding program templates?

If you have microsoft office you can go to their website and they will have free templates of all kinds. Intimateweddings.com also has some templates.


Which type of program allows you to create a newsletter by using templates?

A desktop publishing program or a graphic design software, such as Adobe InDesign or Canva, allows you to create a newsletter using pre-designed templates. These programs provide a user-friendly interface with customizable templates, enabling users to easily insert text, images, and other elements. Many online platforms also offer newsletter-specific templates for email marketing, like Mailchimp or Constant Contact.


What can you use to duplicate a file or program?

Right-click the file/program, click "Copy", right-click where you want the duplicated file/program to go, and click "Paste". ------ Or to duplicate a program you can use Installrite http://www.epsilonsquared.com/


Where can save the date templates be found?

A lot of times the word processing program you are using, such as Microsoft Word, will have templates built right into the program. They allow you to customize and edit the information as you please.


Where can one find calendar templates?

One can find calendar templates by going online to websites or by using a software program such as Microsoft Publisher which includes different calender templates.


What is a duplicate file program or disk stored separately?

A Backup


Where can I get a free wedding templates program?

There are many websites which offer a free wedding templates program. You can try visiting such websites as Off Beat Bride and Wedding Wire for some good examples.


What are wedding program templates good for?

Wedding program templates help the new brides to make important decisions with regards to how the wedding will progress without spending hours struggling to put one together from scratch.


Where can I find free printable invitation templates online?

To make your own free invitation templates you can use software such as Microsoft Word. CorelDraw is another program that will assist you to design your very own templates.


Where can one find free wedding program templates?

Free wedding program templates can be found on multiple websites. Specifically, one should look up big companies like Avery, who produces many templates and stickers. Because these templates are free, this means that one cuts down the cost on invitations and programs. Now, money is only spent on buying the paper, which is inexpensive compared to buying other made templates.


What programs can offer photo fun with templates for facebook?

Deefunia is a site that offer photo fun with templates for facebook. You can also use a program like photoshop, it offer a photo fun with templates for facebook too.