By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

C#-APP: Object Comparison algorithm

P: 71
hi everyone, can you please help me on this.
I am trying to create a function that compares the values inside an array.
(In this case I just an example for an integer). The contents of the array are user inputs. Whenever the function encounters a value that is the same with other values inside the array it should overwrite the other values that are not equal to him.

In my example the result of the array should be {1,1,1} because it encounters a same value '1'. There will always be 3 values inside the array.

Expand|Select|Wrap|Line Numbers
  1. int[] inputs = { 1, 2, 1 };
  2.             int holder = 0;
  3.             bool equalresult = false;
  4.             for (int objctr = 0; objctr < inputs.Length; objctr++)
  5.             {
  6.                 holder = inputs[objctr];
  7.                 for (int inobjctr = objctr + 1; inobjctr < inputs.Length; inobjctr++)
  8.                 {
  9.                     equalresult = int.Equals(inputs[objctr],inputs[inobjctr]);
  10.  
  11.                     if (equalresult)
  12.                     {
  13.                         holder = inputs[inobjctr];
  14.                     }
  15.                 }
  16.             }
  17.  

thanks a lot in advance.
Jan 5 '09 #1
Share this Question
Share on Google+
4 Replies


nukefusion
Expert 100+
P: 221
Have you got C#/.NET 3.0?
If so you can probably do what you want with one line of code using LINQ and lambda expressions. Something like this to get the first duplicated number in the list:

Expand|Select|Wrap|Line Numbers
  1. int firstDuplicate = inputs.First(item => inputs.Count(itemToCount => itemToCount.Equals(item)) > 1);
  2.  
Otherwise, using your existing code you could create a boolean flag outside of your loops to record whether or not a duplicated number has been found and another integer variable to record the number itself. If within the inner loop a match is found, then set this flag to true and save the duplicated number in the variable. After each iteration of the inner loop, check if the flag is set and break out of the outer loop if it is.
After that, just loop back through the array and reset all of the numbers to the duplicate number you found.
Jan 5 '09 #2

P: 71
Thanks for the reply.

I got it working.


I already got the .NET 3.5
but I think i will stick with the old fashion way.
I have not yet used LINQ so I am not yet comfortable about it.
Jan 6 '09 #3

100+
P: 344
Hi old-fashion man dantz,
You don't overwrite any value in the array. you just compare and hold, no assignment appears in your code. try this - if i understood you.

int[] inputs = { 1, 2, 1 };
int? holder = null;
bool equalresult = false;
for (int objctr = 0; objctr < inputs.Length; objctr++)
{
for (int inobjctr = objctr + 1; inobjctr < inputs.Length; inobjctr++)
{
equalresult = int.Equals(inputs[objctr], inputs[inobjctr]);

if (equalresult)
{
holder = inputs[inobjctr];
break;
}
}
if (equalresult)
break;
}
if (holder != null)
for (int i = 0; i < inputs.Length; i++)
inputs[i] = (int)holder;

for (int i = 0; i < inputs.Length; i++)
Console.Write(inputs[i] + " ");
Jan 6 '09 #4

P: 71
Thanks Bassem.
That works pretty well.
:-)
Jan 7 '09 #5

Post your reply

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