469,641 Members | 1,116 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

removing duplicate entries in an array

Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input
into an array. Write a function that processes the array so that any duplicate values are eliminated. Write
an output function that prints out the values of the array. You can assume that there are no more than 20
integers in the input. But there may be less. Zero signifies the end of input. The zero should NOT be
printed.

For example, when the following input is provided to your program:

5 6 22 5 22 7 6 0

your program should print:

5 6 22 7

heres what I got and I tried a few things to remove the dupes but it wouldn't even compile I cant figure it out. What I have is a program that takes in the entries and prints them out but I need some help removing the dupes. ANy help will be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6.     int numbs[20], value, dupes, idx;
  7.     cout << "Please enter in a series of numbers ending it with a 0: ";
  8.  
  9.     for(idx = 0; idx < 20; idx++) {
  10.         cin >> value;
  11.         if(value == 0) {
  12.             break;
  13.         }
  14.         numbs[idx] = value;
  15.     }
  16.     int n = idx;
  17.  
  18.     for (idx = 0; idx < n; idx++)
  19.     {
  20.         if (numbs[idx] != 0)
  21.  
  22.             cout << numbs[idx]<< " " ;
  23.     }
  24.     cout << endl;
  25. }
  26.  
Apr 11 '07 #1
12 20370
Ganon11
3,652 Expert 2GB
What you have posted here compiles fine - after I add a return 0; statement at the end. Was there a problem for you with this part, or with the as-yet-unseen duplicate-removal portion?
Apr 11 '07 #2
ilikepython
844 Expert 512MB
Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input
into an array. Write a function that processes the array so that any duplicate values are eliminated. Write
an output function that prints out the values of the array. You can assume that there are no more than 20
integers in the input. But there may be less. Zero signifies the end of input. The zero should NOT be
printed.

For example, when the following input is provided to your program:

5 6 22 5 22 7 6 0

your program should print:

5 6 22 7

heres what I got and I tried a few things to remove the dupes but it wouldn't even compile I cant figure it out. What I have is a program that takes in the entries and prints them out but I need some help removing the dupes. ANy help will be greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6.     int numbs[20], value, dupes, idx;
  7.     cout << "Please enter in a series of numbers ending it with a 0: ";
  8.  
  9.     for(idx = 0; idx < 20; idx++) {
  10.         cin >> value;
  11.         if(value == 0) {
  12.             break;
  13.         }
  14.         numbs[idx] = value;
  15.     }
  16.     int n = idx;
  17.  
  18.     for (idx = 0; idx < n; idx++)
  19.     {
  20.         if (numbs[idx] != 0)
  21.  
  22.             cout << numbs[idx]<< " " ;
  23.     }
  24.     cout << endl;
  25. }
  26.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. function sort:
  2. declare array2[size]
  3. loop(for every number in array)
  4.     if number not in array2: add number to array2
  5. return array2
  6.  
If you need help just ask.
Apr 11 '07 #3
What you have posted here compiles fine - after I add a return 0; statement at the end. Was there a problem for you with this part, or with the as-yet-unseen duplicate-removal portion?
as ganon11 have said, i guess having the return 0; statement would make the program run..
Apr 11 '07 #4
ok heres what i got now. I don't know how to say if not in array 2 in my if statement. also Im not 100% i wrote the function the right way but I think it's close.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sort(int a[], int size);
  5.  
  6. int main ()
  7. {
  8.     int numbs[20], value, idx;
  9.     cout << "Please enter in a series of numbers ending it with a 0: ";
  10.  
  11.     for(idx = 0; idx < 20; idx++) {
  12.         cin >> value;
  13.         if(value == 0) {
  14.             break;
  15.         }
  16.         numbs[idx] = value;
  17.     }
  18.     int n = idx;
  19.  
  20.  
  21.     for (idx = 0; idx < n; idx++)
  22.     {
  23.         if (numbs[idx] != 0)
  24.  
  25.             cout << numbs[idx]<< " " ;
  26.     }
  27.     cout << endl;
  28. }
  29. int sort(int a[], int size);{
  30.     int array2[];
  31.     for (int i = 0; i < size; i++){
  32.         if ( a[i]
  33.  
Apr 11 '07 #5
also im not having problems compiling it works fine Im just trying to figure out a way to get rid of the duplicates and I'm on pace but I can't figure out the rest of the function to do so. I figured out I need to just put them in another array but I don't knowhow to say if not in array 2.
Apr 11 '07 #6
ilikepython
844 Expert 512MB
also im not having problems compiling it works fine Im just trying to figure out a way to get rid of the duplicates and I'm on pace but I can't figure out the rest of the function to do so. I figured out I need to just put them in another array but I don't knowhow to say if not in array 2.
To search if something is in an array or not you can use various searching algorithms. You can declare a function that goes like this:
Expand|Select|Wrap|Line Numbers
  1. bool function search: (array to search in, item to search for)
  2. loop for item in array
  3.     if item equals current arrayindex return true
  4. else
  5.     return false
  6.  
also I recommend this page:
http://cprogramming.com/discussionar...searching.html
it discusses various ways to search an array.
Hope that helps.
Apr 11 '07 #7
i guess all you have to do is create a loop for inputting a set of numbers in an array.

next loop will print, remove duplicate nad check for 0 all in one shot..

like:

Expand|Select|Wrap|Line Numbers
  1. for (loopCounter = 0; loopCounter <= numberOfInts; loopCounter ++)
  2.      if(array[numberOfInts] != 0)
  3.  
Apr 11 '07 #8
i guess all you have to do is create a loop for inputting a set of numbers in an array.

next loop will print, remove duplicate nad check for 0 all in one shot..

like:

Expand|Select|Wrap|Line Numbers
  1. for (loopCounter = 0; loopCounter <= numberOfInts; loopCounter ++)
  2.      if(array[numberOfInts] != 0)
  3.  
ok im kind of lost here because it's looking like their are a few diffent ways to do this which Im sure their are but I need to write a function. If I could find a way to turn the second of the two duplicates or however many their are to a 0 Im pretty sure my code will throw them out because of the code i have written at the end of main where it says if numbs[idx] != 0 . I just need to figure out a function. Im not sure if a bool function will accomplish this or if I should write a sort function where it puts all the numbers that arent already in array2 into it. Which means it will put all the numbers in once but I got stuck as you could see above.
Apr 11 '07 #9
ok im kind of lost here because it's looking like their are a few diffent ways to do this which Im sure their are but I need to write a function. If I could find a way to turn the second of the two duplicates or however many their are to a 0 Im pretty sure my code will throw them out because of the code i have written at the end of main where it says if numbs[idx] != 0 . I just need to figure out a function. Im not sure if a bool function will accomplish this or if I should write a sort function where it puts all the numbers that arent already in array2 into it. Which means it will put all the numbers in once but I got stuck as you could see above.
im sorry about my previous post..i accidentally pressed tab then enter on my keyboard thinking im still typing in bloodshed..haha!

i guess a bool function might work..you put conditions like (is array[x] = 0) then (is array[x] = previous array[x]'s?)..that might work.. =D
Apr 11 '07 #10
Ok i felt like i got it but then when i compiled it using g++ (GNU) I got the following errors:
accountant.cpp: In function `int main()':
accountant.cpp:29: error: invalid types `int[int]' for array subscript
accountant.cpp:31: error: invalid types `int[int]' for array subscript
accountant.cpp: At global scope:
accountant.cpp:36: error: expected unqualified-id before '{' token

I feel like the function I wrote should be working but I can't understand exactly what the compiler is trying to say...If anyone can look at my function and tell me whats wrong with it I would gretly appreciate it. Im going to keep trying tho thanks!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "accfuncs.cpp"
  3. using namespace std;
  4.  
  5. int sort(int a[], int size);
  6. int main ()
  7. {
  8.     int numbs[20], value, dupes, idx;
  9.     cout << "Please enter in a series of numbers ending it with a 0: ";
  10.  
  11.     for(idx = 0; idx < 20; idx++) {
  12.         cin >> value;
  13.         if(value == 0) {
  14.             break;
  15.         }
  16.         numbs[idx] = value;
  17.     }
  18.     int n = idx;
  19.     int numbs2 = sort(numbs, n);
  20.  
  21.     for (idx = 0; idx < n; idx++)
  22.     {
  23.         if (numbs2[idx] != 0)
  24.  
  25.             cout << numbs2[idx]<< " " ;
  26.     }
  27.     cout << endl;
  28. }
  29.  
  30. int sort(int a[], int size);{
  31.     int array2[];
  32.     for (int i = 0; i < size; i++){
  33.         for ( int j = 0; j < size; j++){
  34.             if (a[i] != array[j])
  35.                 array2[] += a[i];
  36.         }
  37.     }
  38.     return array2;
  39. }
  40.  
Apr 11 '07 #11
Ok i felt like i got it but then when i compiled it using g++ (GNU) I got the following errors:
accountant.cpp: In function `int main()':
accountant.cpp:29: error: invalid types `int[int]' for array subscript
accountant.cpp:31: error: invalid types `int[int]' for array subscript
accountant.cpp: At global scope:
accountant.cpp:36: error: expected unqualified-id before '{' token

I feel like the function I wrote should be working but I can't understand exactly what the compiler is trying to say...If anyone can look at my function and tell me whats wrong with it I would gretly appreciate it. Im going to keep trying tho thanks!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "accfuncs.cpp"
  3. using namespace std;
  4.  
  5. int sort(int a[], int size);
  6. int main ()
  7. {
  8.     int numbs[20], value, dupes, idx;
  9.     cout << "Please enter in a series of numbers ending it with a 0: ";
  10.  
  11.     for(idx = 0; idx < 20; idx++) {
  12.         cin >> value;
  13.         if(value == 0) {
  14.             break;
  15.         }
  16.         numbs[idx] = value;
  17.     }
  18.     int n = idx;
  19.     int numbs2 = sort(numbs, n);
  20.  
  21.     for (idx = 0; idx < n; idx++)
  22.     {
  23.         if (numbs2[idx] != 0)
  24.  
  25.             cout << numbs2[idx]<< " " ;
  26.     }
  27.     cout << endl;
  28. }
  29.  
  30. int sort(int a[], int size);{
  31.     int array2[];
  32.     for (int i = 0; i < size; i++){
  33.         for ( int j = 0; j < size; j++){
  34.             if (a[i] != array[j])
  35.                 array2[] += a[i];
  36.         }
  37.     }
  38.     return array2;
  39. }
  40.  
for line 31 error, i guess what your trying to put there is


if (a[i] != array2[j]) and not
if (a[i] != array[j])
for line 36 error, you will have to omit the excess brace.

and is this statement correct cause im not quite sure about it..havent got bloodshed right now so i cant run this for you..try omitting either the two braces or the semicolon on this part.
int sort(int a[], int size);{
Apr 11 '07 #12
Ganon11
3,652 Expert 2GB
Here's something that's not being picked up by your compiler:

When you say array2[] += a[i], this will not work as you want. You will have to manually add the value a[i] to the array by keeping track of an index j such that array2[j] is the last number in the list. When you want to add an element, you can set array2[j + 1] to a[i], and then increment j.
Apr 11 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by sri2097 | last post: by
5 posts views Thread by Chris Lasher | last post: by
7 posts views Thread by Jim Carlock | last post: by
1 post views Thread by gaikokujinkyofusho | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.