C#C
C#2y ago
ByGoalZ

IndexOutOfRange Leetcode help

Hey,
having an issue with my leetcode problem.
I know that its supposed to be a bitwise XOR operation but I tried a different algorithm as im not that advanced yet.
"Given a non-empty array of integers nums, every element appears twice except for one. Find that single one."

public class Solution {
    public int SingleNumber(int[] nums) {
        int[] nums2 = new int[nums.Length];  
       
        if (nums.Length == 1) {
            return nums[0]; 
        }
        for (int i = 0; i < nums.Length; i++) {
            if (Array.IndexOf(nums2, nums[i]) < 0) {
            nums2[i] = nums[i];
             
            } else {
            var x =  Array.IndexOf(nums2, nums[i]);
                 nums[x] = 0;
                nums[i] = 0;
            }
        }
       
        var index = Array.FindIndex(nums, value => value != 0);
             return nums[index];
        
       
      
        
        
    }
}


Error:Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Was this page helpful?