Ad
  • Custom User Avatar
  • Custom User Avatar

    in my opinion, this is EXACTLY how not to join lists. and i explain why.
    first, if you live in real world, you can come up to really huge lists, which gives you opportunity to hit stack overflow...

    so, i would just travere (loop) the list A, find end, and assign that end to list B
    OR
    i would just assign each node of List B, to List A, similair like this, but in traverse way (with loop), not reccursevlly...

    in languages like c++, the first option would be much nicer, cause you can just reassing node pointers, and find an end amazingly FAST.
    No need to reassign each node which cost in performance heavily.. just connect two worms together...

  • Custom User Avatar

    Benchmarked on modern .NET using BenchmarkDotNet.
    Min() + Max() from LINQ turned out to be faster than a manual for loop in my tests.

    LINQ/BCL methods are heavily optimized nowadays.
    Always benchmark instead of relying on intuition.

    public void Setup()
    {
        var rnd = new Random(42);
        data = Enumerable.Range(0, 1_000_000)
                         .Select(_ => rnd.Next())
                         .ToArray();
    }
    
    public int[] LinqMinMax()
    {
        return new[] { data.Min(), data.Max() };
    }
    
    public int[] ForMinMax()
    {
        int min = data[0];
        int max = data[0];
    
        for (int i = 1; i < data.Length; i++)
        {
            int v = data[i];
            if (v < min) min = v;
            if (v > max) max = v;
        }
    
        return new[] { min, max };
    }
    
    table { border-collapse: collapse; display: block; width: 100%; overflow: auto; } td, th { padding: 6px 13px; border: 1px solid #ddd; text-align: right; } tr { background-color: #fff; border-top: 1px solid #ccc; } tr:nth-child(even) { background: #f8f8f8; }
    
    BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7462/25H2/2025Update/HudsonValley2)
    AMD Ryzen 7 7700 3.80GHz, 1 CPU, 16 logical and 8 physical cores
    .NET SDK 10.0.101
      [Host]     : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v4
      DefaultJob : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v4
    
    Method Mean Error StdDev Allocated
    LinqMinMax 60.04 μs 0.036 μs 0.032 μs 32 B
    ForMinMax 376.83 μs 0.809 μs 0.757 μs 32 B
  • Custom User Avatar

    It's literally part of the instructions that the input string contains only letters.

  • Custom User Avatar
  • Custom User Avatar

    the one liner wizard. Not a pro enough to start doing 1 liner xD

  • Custom User Avatar

    Get ahead with the times, people! /j

  • Custom User Avatar

    This is really cool,but I'll learn it later when I become an expert!

  • Custom User Avatar

    i have no idiea ,absolute code

  • Custom User Avatar
  • Custom User Avatar

    These qs are supposed to be debugged not optimized

  • Custom User Avatar

    writing it in 1 line doesn't make it better...

  • Custom User Avatar

    Super clever!

  • Custom User Avatar

    Contrary to other posters, I believe this is best practice and best performance. the Min Max implementations are vectorized, making them faster than the typical for loop by an order of magnitude. Even considering the double iteration. this will depend on the hardware.

  • Custom User Avatar

    @hiyosilver - did you benchmark your code? I did, and the "naive" Min/Max solution is indeed faster. C# vectorizes the search, which makes it an order of magnitude faster than a linear search with a for loop, according to my benchmark.

  • Loading more items...