Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,162 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,728 people online right now. Registration is fast and FREE... Join Now!




Trapping Duplicates

 
Reply to this topicStart new topic

Trapping Duplicates, Trapping Duplicates

srbjed
post 7 Oct, 2008 - 10:36 AM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

[]
//variable declaration//
int []num;
int duplicate=0,size,temp=0,n=0;



//input//
Console.Write("Input Size: ");
size=int.Parse(Console.ReadLine());
num = new int[size];



for (int i = 0; i < size; i++)
{
do
{
duplicate = 1;
Console.Write("Input a number[" + i + "]: ");
num[i] = int.Parse(Console.ReadLine());
if (temp == num[i])
{
duplicate = 0;
Console.WriteLine("It Is A Duplicate");
}
temp = num[i];
} while (duplicate == 0);

}
[]


Above is my code for trapping duplicates...
But it seems that it can only identify duplicates if and only it is being input consecutively...
What i mean is:
<Sample Input/Output>
Input A Number[0]: 1
Input A Number[1]:1
It Is A Duplicate
Input A Number[1]:2
Input A Number[2]:1
<<<<<<<<>>>>>>

As you can see when i input the value 1 on the 3rd index it didn't recognize that value as duplicated...

Looking forward to your kind answers on what is lacking on my code...
thanks....
whatsthat.gif,)
User is offlineProfile CardPM

Go to the top of the page

srbjed
post 7 Oct, 2008 - 11:22 AM
Post #2


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

//variable declaration//
int []num;
int size,temp=0;
bool duplicate;



//input//
Console.Write("Input Size: ");
size=int.Parse(Console.ReadLine());
num = new int[size];

for (int i = 0; i < size; i++)
{
do
{
duplicate = false;
Console.Write("Input a number[" + i + "]: ");
num[i] = int.Parse(Console.ReadLine());
for (int n = 0; n <
i; n++)
{

if (num[i] == num[n])
{
duplicate = true;
Console.WriteLine("It Is A Duplicate");
}

}

}while (duplicate == true);


}

HERE IS THE WORKING CODE...
HAHAHA...
I ANSWERED MY OWN PROBLEM...
HAHAHA...
anyways just sharing...
whatsthat.gif,)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:21PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month