# Removing duplicate indices within a vector, then printing the new vect

Page 1 of 1

## 1 Replies - 1523 Views - Last Post: 11 June 2008 - 12:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=54495&amp;s=913ea4d493e7baff7de30f7b3a7114c9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ProgrammingisMyEnemy

Reputation: 0
• Posts: 1
• Joined: 11-June 08

# Removing duplicate indices within a vector, then printing the new vect

Posted 11 June 2008 - 09:29 AM

Hi, i was wondering if I could get some help with this problem.
Ive been at it for quite a while...possibly because my understand of C++ is not very good or maybe i just suck at it.
Well here it goes,

Problem:

Write the following function, including a main() function, in index.cpp:
vector<int> noDupeIndex(vector<int> indexlist)
which function returns the integers in indexlist, with duplicate indices removed.
For example, if the vector indexlist is: 1 4 9 16 9 7 4 9 11, then the
function noDupeIndex would return the vector: 1 4 9 16 7 11.

Solution:
This is what i have so far...

```#include <iostream>
#include <vector>

using namespace std;

vector<int> noDupeIndex(vector<int> indexlist)
{
vector<int> b(indexlist.size());				   //Defines the size of the new vector
int i = 0;									 //Variable used to count the entries in vector indexlist
int j = 0;									 //Variable used to count the entries in vector B

//While i is less than the total number of entries in vector indexlist the following loop will assign entry n from vector indexlist to entry n of
//vector B. Each time it does this it addes 1 to the entry counters for the respective vectors indexlist and B.
for (int i = 0; i < indexlist.size(); i++)
{
b[j] = indexlist[i];
j++;
}

return b;
}

int main()
{
//This variable is a counting variable used later to manage a loop.
int x;

//Defining vector size and entries for indexlist
vector<int>  indexlist(9);
indexlist[0] = 1;
indexlist[1] = 4;
indexlist[2] = 9;
indexlist[3] = 16;
indexlist[4] = 9;
indexlist[5] = 7;
indexlist[6] = 4;
indexlist[7] = 9;
indexlist[8] = 11;

vector<int> b = noDupeIndex(indexlist);

cout << "Result of no duplicate index is:  ";

//  While the counting variable is less than the total length of vector B, the following loop displays that corresponding entry.
//Everytime the loop is run, a integer of 1 is added to the counter. Thus each proceeding time, a new corresponding entry is displayed
//until there are no more entries left.
for (x = 0; x < b.size(); x++)

cout << b[x] << " ";

cout << "\n";

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Removing duplicate indices within a vector, then printing the new vect

### #2 joske

• D.I.C Regular

Reputation: 43
• Posts: 297
• Joined: 04-September 07

## Re: Removing duplicate indices within a vector, then printing the new vect

Posted 11 June 2008 - 12:35 PM

A hint:

```	//While i is less than the total number of entries in vector indexlist the following loop will assign entry n from vector indexlist to entry n of
//vector B. Each time it does this it addes 1 to the entry counters for the respective vectors indexlist and B.
for (int i = 0; i < indexlist.size(); i++)
{
bool exists = false;

// write code here to check if item indexlist[i] already exists in vector b[ ]
// if so, make variable exists true.
// you can use a for loop for this

if (exists == false)
{
b[j] = indexlist[i];
j++;
}
}

```

This post has been edited by joske: 11 June 2008 - 03:18 PM