473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 20755
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

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

Similar topics

3
by: Rad | last post by:
I have a table . It has a nullable column called AccountNumber, which is of varchar type. The AccountNumber is alpha-numeric. I want to take data from this table and process it for my application....
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
4
by: sri2097 | last post by:
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file...
5
by: Chris Lasher | last post by:
Hello Pythonistas! I'm looking for a way to duplicate entries in a symmetrical matrix that's composed of genetic distances. For example, suppose I have a matrix like the following: A B ...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
1
by: JTreefrog | last post by:
Hello - I've read a ton of stuff about deleting duplicate values in an array. They are all very useful - they just haven't addressed an array of objects. Here's my array: var sDat = ; The...
1
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.