C
C#ā€¢3mo 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];





}
}
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.
75 Replies
TheRanger
TheRangerā€¢3mo ago
which line gave you that error?
ByGoalZ
ByGoalZā€¢3mo ago
It doesnt show, im using the leetcode editor so :/
TheRanger
TheRangerā€¢3mo ago
well that error only appears when you're trying to access the array out of its bounds
ByGoalZ
ByGoalZā€¢3mo ago
it also doesnt show me for which testcase its failing
TheRanger
TheRangerā€¢3mo ago
eg if array's size is 5 and you tried to access element 6, u get this error you can only pick elements from 1 to 5
ByGoalZ
ByGoalZā€¢3mo ago
Yea, chatgpt said it might be due to this line "var index = Array.FindIndex(nums, value => value != 0);" as it will return -1 if there is no element thats not zero
TheRanger
TheRangerā€¢3mo ago
yeah no i wouldnt rely on chatgpt 100%
ByGoalZ
ByGoalZā€¢3mo ago
but there must be an element thats not zero Yea ik but I have no idea what else it could be
TheRanger
TheRangerā€¢3mo ago
maybe the arrays size is 0
ByGoalZ
ByGoalZā€¢3mo ago
And it worked for 5 testcases so the algorithm shouldnt be completely false
TheRanger
TheRangerā€¢3mo ago
use Console.WriteLine to debug the array's size
ByGoalZ
ByGoalZā€¢3mo ago
well theres written "non-empty arrays" alr
TheRanger
TheRangerā€¢3mo ago
just test
ByGoalZ
ByGoalZā€¢3mo ago
3
TheRanger
TheRangerā€¢3mo ago
this might be the problem
var x = Array.IndexOf(nums2, nums[i]);
var x = Array.IndexOf(nums2, nums[i]);
x is probably -1 then this nums[x] = 0; threw the error or this too
var index = Array.FindIndex(nums, value => value != 0);
return nums[index];
var index = Array.FindIndex(nums, value => value != 0);
return nums[index];
ByGoalZ
ByGoalZā€¢3mo ago
yea this is what chatgpt said but why would it be -1
TheRanger
TheRangerā€¢3mo ago
well it means it didnt find any number that isnt 0 in the array debug the values of the array
ByGoalZ
ByGoalZā€¢3mo ago
alr its [0,0,0] lol how tf
exe
exeā€¢3mo ago
How about step through it with the debugger?
ByGoalZ
ByGoalZā€¢3mo ago
wdym by that?
TheRanger
TheRangerā€¢3mo ago
does leetcode even have a debugger?
ByGoalZ
ByGoalZā€¢3mo ago
Im wondering what happened if it would be [0,2,2], then the final array would be [0,0,0] which would error
exe
exeā€¢3mo ago
OH ok, I have no idea what is leetcode let me have a quick scan of your code
ByGoalZ
ByGoalZā€¢3mo ago
leetcode is a coding challenge website yep this is the issue, original array is [1,0,1]
TheRanger
TheRangerā€¢3mo ago
you are zeroing your array with nums[i] = 0;
ByGoalZ
ByGoalZā€¢3mo ago
but how can I solve it
exe
exeā€¢3mo ago
what is this even supposed to do?
ByGoalZ
ByGoalZā€¢3mo ago
"Given a non-empty array of integers nums, every element appears twice except for one. Find that single one." yes, is that wrong?
TheRanger
TheRangerā€¢3mo ago
why are you zeroing the values of your array?
exe
exeā€¢3mo ago
and this is your code?
ByGoalZ
ByGoalZā€¢3mo ago
every zero is an element thats double, so at the end only the single element is still there, all others are zero. Thats why at the end I return the element that isnt zero Yes, but im aware that its normally done with a XOR expression but I tried doing it on my own
exe
exeā€¢3mo ago
No linq?
ByGoalZ
ByGoalZā€¢3mo ago
no , this would be linq with the XOR expression, but I just read that online. public int SingleNumber(int[] nums) => nums.Aggregate((s, a) => s ^ a);
TheRanger
TheRangerā€¢3mo ago
i dont think you need to do bitwise XOR operation just count the values, return the one whose count is 1
exe
exeā€¢3mo ago
I'm sure it's just something like array.Single(x => array.Count(... that sort of thing
TheRanger
TheRangerā€¢3mo ago
GroupBy should work too
exe
exeā€¢3mo ago
yeah
ByGoalZ
ByGoalZā€¢3mo ago
wdym count? How do I remember the ones before like, how do I store them
TheRanger
TheRangerā€¢3mo ago
well, there are many ways like using dictionary, linq, or whatever
exe
exeā€¢3mo ago
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one This to me just reads as, assume every number in some given array appears twice except for 1 no need to count everything to make sure, just find the thing which there is only 1 of
ByGoalZ
ByGoalZā€¢3mo ago
yea thats what I tried, how would you do it?
TheRanger
TheRangerā€¢3mo ago
if you want to avoid linq, just use a nested for loop or just use linq šŸ¤· But that wouldnt be much of a challenge
exe
exeā€¢3mo ago
No description
exe
exeā€¢3mo ago
a very simple 1 liner
TheRanger
TheRangerā€¢3mo ago
I usually avoid spoonfeeding its not much of a challenge if someone does it for you
exe
exeā€¢3mo ago
My bad, sorry I've just joined šŸ™‚
ByGoalZ
ByGoalZā€¢3mo ago
yea, im not submitting your solutions, just interested. I havent really touched LINQ before so i dont even know what "=>" means šŸ˜‚ is it like an "if?
TheRanger
TheRangerā€¢3mo ago
thats lambda expression you could just use a nested for loop
exe
exeā€¢3mo ago
Ah ok, linq is just a bunch of .NET methods to make things easier to read and write, reducing the need to write for loops yourself in this case. and yes => is a lambda
ByGoalZ
ByGoalZā€¢3mo ago
How?
exe
exeā€¢3mo ago
without linq, you could create exactly the same result by writing for loops yourself. the Single is a method, which iterates through everything in x here. With this fact, you can start your code with a for loop
ByGoalZ
ByGoalZā€¢3mo ago
Ok so: Iterate through the array -> then what? I cant just compare the current element to the next one right?
exe
exeā€¢3mo ago
It also takes a lambda returning a bool, known in most languages as a predicate, a condition
ByGoalZ
ByGoalZā€¢3mo ago
I did it lol
No description
TheRanger
TheRangerā€¢3mo ago
You can start by wrapping your own Count method nvm then lol
ByGoalZ
ByGoalZā€¢3mo ago
Just did this
No description
TheRanger
TheRangerā€¢3mo ago
That's a linq method
ByGoalZ
ByGoalZā€¢3mo ago
yea I have used linq before but just simple expressions I always use the => as an if šŸ˜‚ Imma start to learn it properly tho
TheRanger
TheRangerā€¢3mo ago
That doesnt really answer your challenge you just happened to return 0, which is the correct answer but if leetcode sent you another input, it may not work
ByGoalZ
ByGoalZā€¢3mo ago
no, it tests with hundreds of testcases the issue in my code just was that when the single number is a zero, the logic breaks basically
TheRanger
TheRangerā€¢3mo ago
but your elements arent all zero, so the variable will be false
ByGoalZ
ByGoalZā€¢3mo ago
they are cuz my code is setting every double int to zero, so just the single number will be left, which if its a zero would be an all zero array
exe
exeā€¢3mo ago
so that means, you iterate from the start of x, to the end. At every iteration you need to know if this number appears once, or more. You could do this with a nested for loop. You'll need to count the occurrences of the occurence in the inner loop Looking at your code, I don't think there is a need to use FindIndex here
TheRanger
TheRangerā€¢3mo ago
what do u mean by double int
ByGoalZ
ByGoalZā€¢3mo ago
poor choice of words but every digit that appears twice lol my code is a mess and overcomplicated ik šŸ˜‚
exe
exeā€¢3mo ago
Beginning programming always is, don't worry šŸ™‚
ByGoalZ
ByGoalZā€¢3mo ago
alr, I will try to do it this way and see if the runtime decreases and at the same time its another challenge lol
exe
exeā€¢3mo ago
If you can assume there is only 1 thing in an array which appears once, and the rest twice. You should be able to just have a nested for loop. The inner loop counting the occurences of the element targeted by the outer loop. After the inner loop, but still inside the outer loop, I think you could bail out if ==1
ByGoalZ
ByGoalZā€¢3mo ago
ooh thanks
exe
exeā€¢3mo ago
That's OK, there are, as R ^ said, many ways to do it. The cleanest is with linq but probably better to learn things like this without first
ByGoalZ
ByGoalZā€¢3mo ago
Thanks, this worked even better and way less code. Gonna try it with LINQ too.
No description
exe
exeā€¢3mo ago
No worries, I sketched out something like this; if you wanted to compare
No description
exe
exeā€¢3mo ago
If you're interested in what's likely, or similarly going on inside those LINQ Maybe not quite as naive, but effectively similar
exe
exeā€¢3mo ago
No description
exe
exeā€¢3mo ago
So actually, the for loops might be a little more efficient with 2 iterations instead of 3 here with LINQ.
Want results from more Discord servers?
Add your server
More Posts