473,657 Members | 2,428 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 1633
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(inpu ts[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(i nputs[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
11024
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 such objects. I was trying to use Java refelction to navigate and compare. Thanks
0
953
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 far as customizing our application through python. How ever i am expecting following behavior but it failed. Can some oe
23
25614
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 completed. In order for this to work, I must pass it the form name, and the field names (input tag names). Sometimes I copy/paste the syntax from one page to another and I might forget to change the form name, or an input tag name passed in the
1
2115
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. their free trial versions tend to be limited to only a few of the objects. Bob
8
19787
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 result list's elements to contain a moniker denoting whether that element: 1..Exists only in the reference list or
0
946
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, phone number etc public properties and a property that exposes a list of Accounts. That list of Accounts is an arraylist of Account classes. The Account class has several public properties, including an AuthorisedBuyers arraylist
0
1271
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 Comparison to be used for our general admin purposes. Questions: 1) Is it possible to rename columns e.g. using the compare masks; and
0
1147
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
1251
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
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.