C#-APP: Object Comparison algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantz
    New Member
    • Oct 2008
    • 71

    C#-APP: Object Comparison algorithm

    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.

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

    thanks a lot in advance.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    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:

    Code:
    int firstDuplicate = inputs.First(item => inputs.Count(itemToCount => itemToCount.Equals(item)) > 1);
    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.

    Comment

    • dantz
      New Member
      • Oct 2008
      • 71

      #3
      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.

      Comment

      • Bassem
        Contributor
        • Dec 2008
        • 344

        #4
        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] + " ");

        Comment

        • dantz
          New Member
          • Oct 2008
          • 71

          #5
          Thanks Bassem.
          That works pretty well.
          :-)

          Comment

          Working...