❔ LINQ distinct from 2 lists preserving order as soon as they are seen
I have 2 lists like this
1: {1, 2, 3}
2: {1, 3, 4}
what's the LINQ distinct method for them that provides this output?
{1, 2, 3, 4}
it should be the functional equivalent as this, and not ordering by value
50 Replies
Ero#1111
REPL Result: Success
Result: List<int>
Compile: 525.116ms | Execution: 35.438ms | React with ❌ to remove this embed.
sorry I should've said
doing that on a longer list fails
To compile C# code in Discord, use
!eval
Example:
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!Alan72104#4011
REPL Result: Success
Result: int[]
Compile: 513.491ms | Execution: 55.842ms | React with ❌ to remove this embed.
hm?
I need the distinct values of them, starting from a1[0]/a2[0] to the last values
the 4 is weirdly placed at the last index
Simplest possible solution would probably be to append the lists and then transform that new list into a set
Perhaps
chain
is a thing in LINQthanks for the idea
Ok it's called
Union
it seems
Oh actually
The Union
function actually produces a SET union
So it's actually exactly what you need hahahyeah but, why does that matter?
you didn't specify that they need to be in order
Hah reading up it looks like Ero already used that exact function why am I even here lol
it's updating the top 50 players in the leaderboards of a game
and I want to see the top 10 updated results as soon as possible because updating it has a cooldown
if speed is a concern, linq is the wrong choice
but sure
Ero#1111
REPL Result: Success
Result: List<int>
Compile: 543.488ms | Execution: 38.985ms | React with ❌ to remove this embed.
should have used your numbers
you get the idea
um not the order of values btw
then what are you asking
in the order of appearance
?
like in this looping method, starting to add them from index 0
Union() placed the 4 at the last index, which is false obviously bc there are still many different elements after the 4 in a2
it's not "false"
false to my requirements
so?
Ero#1111
REPL Result: Success
Result: int[]
Compile: 498.878ms | Execution: 52.171ms | React with ❌ to remove this embed.
what's wrong with this?
I did some search but still can't find a good way of doing that
result should be
1
2
3
4
5
6
7
8
9
it is
well um this method is fine with the example arrays
show an example where it isn't fine
Alan72104#4011
REPL Result: Success
Result: string[]
Compile: 549.772ms | Execution: 33.969ms | React with ❌ to remove this embed.
Ero#1111
REPL Result: Success
Result: string[]
Compile: 524.397ms | Execution: 35.454ms | React with ❌ to remove this embed.
not ordering that by value but the order of appearance
what does that even mean
first try adding a1[0]
then a2[0]
then a1[1]
then a2[1]
I had trouble describing that to google so I came here
why does this matter exactly...?
i don't understand...
if the 1st player is being ordered to the last place just because their name starts with "a"
and I have 1000 players to update, each taking a 10 sec cooldown
I would not be seeing the updated first page of the leaderboard in at least 10 * 1000 secs
(the 1st player means the 1st place in the current leaderboard)
plus there are 2 leaderboards, that's why I'm doing the distinct and ordering by appearance
So in your first example, MinionShop would be 3rd?
yes right
Gotcha. I don't think LINQ contains a built in method for that
nope, I googled for a while
Ero#1111
REPL Result: Success
Result: string[]
Compile: 635.542ms | Execution: 78.641ms | React with ❌ to remove this embed.
does that look right?
of course, this is very very bad
not performant, unnecessary allocations
but it's short and works
ok ty I will just stay with this
yep gotten
likely improvable
I will probably make it an extension too since I will keep using it for a while
ty all
a.Concat(b).Distinct().OrderBy(x => x)
not what they want
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.