Arrays HELP!!!

How do you write a function that removes duplicates from an array.
Last edited on
What approach have you tried? Show us some code.
- look at one element in the array
- compare it to all other elements
- for each other element that matches it, remove it
- repeat until all duplicates are removed
Show us the code...

Or do what Disch said above :P
Here is the question I am quizzing myself on but cant remember....Write a function that removes duplicates from an array. For example, if remove_duplicates() is called with an array containing

1 4 9 16 9 7 4 9 11

then the array is changed to

1 4 9 16 7 11

i need my function to have a reference parameter for the array size that is updated when removing the duplicates and to have the main program ask for a list of numbers terminated by a sentinel. Call the function to remove the duplicates then output the new list and the size of the list.

Write a function that removes duplicates from an array. For example, if remove_duplicates() is called with an array containing

1 4 9 16 9 7 4 9 11

then the array is changed to

1 4 9 16 7 11

Your function should have a reference parameter for the array size that is updated when removing the duplicates.

This is what I have so far: #include <iostream>
#include <cmath>

int main()
{
int numbers={1,4,9,16,9,7,4,9,11};

remove_duplicates(numbers);

for(int n=0, n<=6; n++){

cout<<numbers[n]<<"";
}
cout<<endl;
return o;
}


What can I do to correct this and complete what is being asked?

What I would do is sort the array so it is in numerical order. Then if (a[i]==a[i-1) a[i] would be a duplicate.
This is what I have now:
int main()
{
int list [9]= {1,4,9,16,9,7,4,9,11};

int num=9;

int dup=0;

int i,n;

for (i=0; i<num; i++)
{
for (n=0; n<i; n++)
{
if (list[i] == list [n])
{
dup =9,4;
}
else
{
if (!dup)
{
dup=0;
}
}
}
}
if (dup)
{
cout<<Dupicate found<<""<<endl;
}
else
{
cout<<No Duplicate found<<""<<endl;
}
return 0;
}

but im getting these errors:In function ‘int main()’:
error: ‘cout’ was not declared in this scope
error: ‘Dupicate’ was not declared in this scope
error: expected ‘;’ before ‘found’
error: ‘cout’ was not declared in this scope
error: ‘No’ was not declared in this scope
error: expected ‘;’ before ‘Duplicate’


How do I declare cout?
#include <iostream>
using namespace std;
so I fixed my errors but now my output says either duplicate or not duplicate and I need it to output the list of numbers without any
duplicates in it. How do I do this?
I'm pretty sure your program isn't suppose to enter the nine numbers in the directions. You are supposed to make it so any numbers you enter delete the duplicates.

This might help a bit:
http://www.cplusplus.com/forum/beginner/24168/

1
2
3
4
5
6
7
8
9
10
for (int i = 1; i < n; i++)
	{
		bool matching = false;
		for (int j = 0; (j < i) && (matching == false); j++)
			if (numbs[i] == numbs[j]) 
				matching = true;
		
		 if (!matching)
		cout << numbs[i] << " ";
	}


in previous code it removes duplicates of one array ,
I want it in 2D array .. !!
Do I have to add one more "for loop" ?


First of all you need to get your function started instead of doing everything in main(). If after you modify the array you need to print it out, that means your size variable you send to the function has to be able to be modified.

void remove_dup(int a[], int & size); // declaration

So if size is originally 9, each time you remove a duplicate in the function, do size--;. Then when you're back in main you can print the contents with a for loop that goes to i < size and it will work.

Inside the function:
- use a for loop starting at i = 0 to compare a[i] with the rest of a[]
- an inner loop goes through the rest of the array (starting at j = i + 1) to compare
- if duplicate found, use a 3rd loop to shift remaining array elements down one, overwriting the duplicate; then decrement size (size--)

See if you can make something like that work before someone just gives you the answer. :)
Okay I changed my code and put it in bool form and here it is
#include <iostream>
#include <cmath>
using namespace std;
const int SIZE=20;

int main ()
{
int numbs[SIZE],i,n;
cout<<"Please enter size of an array"<<endl;
cin>>n;
cout<<"Please enter in a series of numbers"<<endl;

for (i=0; i < n; i++)
cin>>numbs[i];


cout<<numbs[0]<<"";

for (int t=1; t<n; t++)
{

bool matching = false;
for (int j=0; (j<1) && (matching == false); j++) if (numbs[t] == numbs[j]) matching=true;
if (!matching)
cout<<numbs[t]<<"";

}

return(0);

}


but when I do a.out
it asks for the size and numbers but doesnt output the list of numbers without duplicates but a completely different list of numbers How do I fix this?
closed account (D80DSL3A)
Ignored advice removed.
Last edited on
//
//
//Function that removes duplicates from an array
//
//
#include <iostream>
#include <cmath>
using namespace std;


int main ()
{
int sentinel =-99;
int numbs[20], i, n;


cout<<"Please enter in a series of numbers"<<endl;

i=0;
cin>>numbs[i];
while (numbs[0]!=-99)

cout<<numbs[0]<<"";

for (int t=1; t<n; t++)
{

bool matching = false;
for (int j=0; (j<1) && (matching == false); j++)
if (numbs[t] == numbs[j]) matching=true;
if (!matching)
cout<<numbs[t]<<endl;

}

return(0);

}


this is what i have but when i do a.out it asks to enter a series of numbers and I do then it just puts the numbers never ending across the screen.what do i do to get my sentinel to stop the lists and for it to remove the duplicates?
Topic archived. No new replies allowed.