changing a boolean value outside a loop from inside a loop
so im doing a leetcode challenge to detect duplicate numbers in an array, im trying to use the duplicate bool value so when a duplicate is detected, it edits the duplicate value to true and exits the loop, no matter what i try the output is always false however
11 Replies
from what i can see
when i try and set duplicate to true
it is not accessing the variable outside the loop
any help appreciated im still new
Or, what is much more likely than the language completely breaking,
count2
is never more than 1its probably not the most efficient way of solving the problem
how?
it loops through whole array and increases count everytime there is a same number
Well, the easiest way to figure it out is to place some breakpoints and debug it
leetcode isnt good for that is it
i dont have vs on this pc
it only shows me my outputs
for each test case
when i run it
ill try it on studio tomorrow
Then trying to figure it out really won't be easy
yh
You can run this code on Sharplab and do some good ol'
Console.WriteLine()
debuggingill just watch a tutorial ig
p[robably a more efficient way anyway
thx tho
As stated, when a duplicate is found, count2 is incremented once; then, the condition for the bool change is
count2 > 1
, which won’t happen ever.
Maybe the condition could be count2 >= 1
?
Or maybe you can use LINQ, with bool duplicate = nums.ToLookup(n => n).Any(g => g.Count() > 1);
(requires using System.Linq
as a header), instead of the entire while
block?@zzz if you want to briefly test some C# code, you can use sharplab.io, it’s an online dev pad, where you'll find nearly everything you need for proof of concepts.
SharpLab
C#/VB/F# compiler playground.