C#C
C#15mo ago
zzz

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

public class Solution 
{

    int[] nums ={1, 2, 3, 1};

    public bool ContainsDuplicate(int[] nums)
    {
        bool running = true;
        bool duplicate = false;
        int count = 0;
        int count2 = 0;

        while (running == true)
        {
            foreach (int x in nums)
            {
                foreach (int z in nums)
                {
                    
                    if (z == nums[count])
                    {
                        count2++;
                        break;
                    }
                    
                    count++;
                }
                if (count2 > 1)
                    {
                        running = false;
                        duplicate = true;
                    }
                count2 = 0;
                
            }
            running = false;            
        }
        return duplicate;
    }
}
Was this page helpful?