473,503 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#-APP: Object Comparison algorithm

71 New Member
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
4 1628
nukefusion
221 Recognized Expert New Member
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
dantz
71 New Member
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
Bassem
344 Contributor
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
dantz
71 New Member
Thanks Bassem.
That works pretty well.
:-)
Jan 7 '09 #5

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

Similar topics

3
11019
by: Dheeraj | last post by:
Hi : I am writing a test utility where i wish to compare two Java objects. The object consists of many other objects. In other words the objects tree is deep. Does anyone one how to compare...
0
942
by: Anand K Rayudu | last post by:
Dear All, I have some question regarding embedding and exposing of C pointers. We have embedded python and extended to expose our APIs and c objects to python. Every thing is working fine as...
23
25593
by: Randell D. | last post by:
Folks, I have written some scripting tools that are compatable with alot of my pages - For example, I've created a script that will check to ensure certain form fields that require data, are...
1
2111
by: Bob Alston | last post by:
Anybody know of any totally FREE Access object comparators which will compare the database objects in two databases and show the differences? There are several commercial products that do this. ...
8
19780
by: Ben Fidge | last post by:
I have a requirement to compare two lists, generating a third which is a combination of both. With the first list as the reference list and the second as the test list, I want the each of the...
0
937
by: Ben Fidge | last post by:
I've got a hierachy of classes and want to iterate through two instances and return the differences between them. Take the following scenario. I have a class called Customer. It contains name,...
0
1254
by: juliane26 | last post by:
Even so it should not be there, it sometimes is: somebody wants to rename a column. Today we do unload / recreate table / load using our own db utilities to do so. Now we evaluate Object...
0
1142
by: amitjenny | last post by:
Can anyone povide me the document for db2 object comparison tool frm v2.1 to v3.1 for the db2 v8 mgration. I was not able to find it on internet. Quick response is appreciated. Thanks, Amit
11
1233
by: A.T.Hofkamp | last post by:
Hello all, Yesterday we found the cause of a bug that has caused problems for a long time. It appeared to be the following: class A(object): pass print min(1.0, A())
0
7202
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
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.