0% found this document useful (0 votes)
35 views

Remove Duplicates From An Array - C++ Forum

The document is about removing duplicate integers from an array in C++. A user provides a sample input of integers with duplicates. The goal is to print the unsorted list without duplicates. The original code posted by the user does not work correctly, either removing the last integers or adding random values. Another forum member provides a corrected code that uses a boolean variable to check if the current integer being checked has already appeared in the array, and only prints it if it has not been seen before. This solves the problem of removing duplicate integers from the input array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Remove Duplicates From An Array - C++ Forum

The document is about removing duplicate integers from an array in C++. A user provides a sample input of integers with duplicates. The goal is to print the unsorted list without duplicates. The original code posted by the user does not work correctly, either removing the last integers or adding random values. Another forum member provides a corrected code that uses a boolean variable to check if the current integer being checked has already appeared in the array, and only prints it if it has not been seen before. This solves the problem of removing duplicate integers from the input array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Search:

Forum
C++
Information
Tutorials
Reference
Articles
Forum

Beginners

Go
Removeduplicatesfromanarray

Notloggedin

register

login

Removeduplicatesfromanarray
haim(9)

May23,2010at11:23am

Hi,Igotaproblemwithremovingduplicateintsfromanarray.
Problemisthatuserwilltypeintsasmanyasheorshecan.
Theprogramneedtoprintoutunsortedlistofnumbersafterremovingduplicates.
Forum

Beginners
WindowsProgramming
UNIX/LinuxProgramming
GeneralC++Programming
Lounge
Jobs

eg.usertype:242848
Thenneedtoprintoutlikethis:248
Thismycodebutitdoesn'tworkcorrectlyiteitherremoveslastintegersoraddsrandomvalues.
1 #include<iostream>
2 usingnamespacestd;
Edit
3 constintSIZE=20;
4 intmain()
5 {
6 intnumbs[SIZE],value,idx,n;
7 cout<<"PLeaseentersizeofanarray"<<endl;
8 cin>>n;
9 cout<<"Pleaseenterinaseriesofnumbers"<<endl;
10 for(idx=0;idx<n;idx++)
11 cin>>numbs[idx];
12
13
14 for(idx=0;idx<n;idx++)
15 {
16 for(value=n;value>idx;value)
17 if(numbs[idx]==numbs[value])
18 {
19 numbs[value]=numbs[value+1];
20 n=n1;
21 }
22 }
23
24 for(idx=0;idx<=n;idx++)
25 {
26 cout<<numbs[idx]<<"";
27 }
28 system("pause");
29 return0;
30 }

screw(145)

May23,2010at12:15pm

Hello!
From14line:
1
2
3
4
5
6
7

cout<<numbs[0]<<"";
for(inti=1;i<n;i++)
{

boolmatching=false;

for(intj=0;(j<i)&&(matching==false);j++)if(numbs[i]==numbs[j])matching=true;

if(!matching)cout<<numbs[i]<<"";
}

Thefirstnumberisprintedoutbecauseitdoesn'tactbefore.
Inthefirstloopyoushouldcheckallnumberfrom1tilln:
for(inti=1;i<n;i++)
Variablematchingisusedforstoringthematchingundercheckingprocess.
Inthenextloopyoushouldcheckthatthecheckednumberactedbefore.Ifitistruethanitwillbenotedinvariable
matching.
Finallyyourcode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include<iostream>
usingnamespacestd;
constintSIZE=20;
intmain()
{
intnumbs[SIZE],value,idx,n;
cout<<"PLeaseentersizeofanarray"<<endl;
cin>>n;
cout<<"Pleaseenterinaseriesofnumbers"<<endl;
for(idx=0;idx<n;idx++)
cin>>numbs[idx];

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

for(inti=1;i<n;i++)

boolmatching=false;

for(intj=0;(j<i)&&(matching==false);j++)if(numbs[i]==numbs[j])matching=true;

if(!matching)cout<<numbs[i]<<"";

Topicarchived.Nonewrepliesallowed.

Edit

Homepage|Privacypolicy
cplusplus.com,20002016Allrightsreservedv3.1
Spottedanerror?contactus

You might also like