Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:48:43+00:00 2026-05-27T14:48:43+00:00

Say I have elements (X, Y, and Z) in a list, I have a

  • 0

Say I have elements (X, Y, and Z) in a list, I have a function, that generates a percentage, of how much two objects resemble each other.

What I want to do, is run X against Y and Z using my compareElements, so:

compareElements(X,Y); // equals 55
compareElements(X,Z); // equals 60

Then Y against X and Z

compareElements(Y,X); // equals 55
compareElements(Y,Z); // equals 62

Then Z against Y and X

compareElements(Z,X); // equals 60
compareElements(Z,Y); // equals 62

Then, I return the highest value, which is 62.

Obviously, there’s some repetition there, I don’t need the repetition, but I’m not sure how to eliminate it.

How do I structure my LINQ query, or a function/algorithm to do this comparison on every element, without the repetition?

I’d prefer to use LINQ if I can, as I’m being passed an enumerable and the function returns before the list is actually enumerated, so we can save the cost of performing the compare, until the list is enumerated.

All I need is that highest value, of the compare functions, 62.

Note: My actual result set I’m working with averages between 3 and 10 elements in the list, that need to be ran through this compare function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:48:44+00:00Added an answer on May 27, 2026 at 2:48 pm

    For the sake of readability, I would write an iterator block to generate the comparisons in a non-repetitive manner:

    IEnumerable<Tuple<T, T>> GetComparisons<T>(IEnumerable<T> elements)
    {
        var visited = new List<T>();
    
        foreach(T current in elements)
        {
            foreach(T previous in visited) 
                yield return new Tuple<T, T>(current, previous);
    
            visited.Add(current);
        }
    }
    

    Then you can do the following:

    var highScore = GetComparisons(listOfElements)
                        .Select(x=>compareElements(x.Item1, x.Item2)
                        .Max();
    

    (That said I prefer Smelch’s suggestion for situations where there’s no practical reason to use LINQ or iterators, such as having a need for composable routines.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list in R some 10,000 elements long. Say I want to
Let's say I have a list of User objects with two properties...ID and Name
let's say i have a list with some elements and i want to choose
If I have a short list (let's say two or three elements) I would
Say I have a list and I want it arranged so that the sum
Say we want to make a function like minBy that returns all elements of
Say you have some list L and you want to split it into two
Say we have a standard WPF-application, and I want to show some UI-elements on
Lets say there are two pages with two different html elements which have the
Let's say I have a function that takes a string. That string contains the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.