C
C#ā€¢9mo 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ā€¢9mo ago
which line gave you that error?
ByGoalZ
ByGoalZOPā€¢9mo ago
It doesnt show, im using the leetcode editor so :/
TheRanger
TheRangerā€¢9mo ago
well that error only appears when you're trying to access the array out of its bounds
ByGoalZ
ByGoalZOPā€¢9mo ago
it also doesnt show me for which testcase its failing
TheRanger
TheRangerā€¢9mo 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
ByGoalZOPā€¢9mo 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ā€¢9mo ago
yeah no i wouldnt rely on chatgpt 100%
ByGoalZ
ByGoalZOPā€¢9mo ago
but there must be an element thats not zero Yea ik but I have no idea what else it could be
TheRanger
TheRangerā€¢9mo ago
maybe the arrays size is 0
ByGoalZ
ByGoalZOPā€¢9mo ago
And it worked for 5 testcases so the algorithm shouldnt be completely false
TheRanger
TheRangerā€¢9mo ago
use Console.WriteLine to debug the array's size
ByGoalZ
ByGoalZOPā€¢9mo ago
well theres written "non-empty arrays" alr
TheRanger
TheRangerā€¢9mo ago
just test
ByGoalZ
ByGoalZOPā€¢9mo ago
3
TheRanger
TheRangerā€¢9mo 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
ByGoalZOPā€¢9mo ago
yea this is what chatgpt said but why would it be -1
TheRanger
TheRangerā€¢9mo ago
well it means it didnt find any number that isnt 0 in the array debug the values of the array
ByGoalZ
ByGoalZOPā€¢9mo ago
alr its [0,0,0] lol how tf
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
How about step through it with the debugger?
ByGoalZ
ByGoalZOPā€¢9mo ago
wdym by that?
TheRanger
TheRangerā€¢9mo ago
does leetcode even have a debugger?
ByGoalZ
ByGoalZOPā€¢9mo ago
Im wondering what happened if it would be [0,2,2], then the final array would be [0,0,0] which would error
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
OH ok, I have no idea what is leetcode let me have a quick scan of your code
ByGoalZ
ByGoalZOPā€¢9mo ago
leetcode is a coding challenge website yep this is the issue, original array is [1,0,1]
TheRanger
TheRangerā€¢9mo ago
you are zeroing your array with nums[i] = 0;
ByGoalZ
ByGoalZOPā€¢9mo ago
but how can I solve it
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
what is this even supposed to do?
ByGoalZ
ByGoalZOPā€¢9mo 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ā€¢9mo ago
why are you zeroing the values of your array?
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
and this is your code?
ByGoalZ
ByGoalZOPā€¢9mo 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
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
No linq?
ByGoalZ
ByGoalZOPā€¢9mo 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ā€¢9mo ago
i dont think you need to do bitwise XOR operation just count the values, return the one whose count is 1
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
I'm sure it's just something like array.Single(x => array.Count(... that sort of thing
TheRanger
TheRangerā€¢9mo ago
GroupBy should work too
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
yeah
ByGoalZ
ByGoalZOPā€¢9mo ago
wdym count? How do I remember the ones before like, how do I store them
TheRanger
TheRangerā€¢9mo ago
well, there are many ways like using dictionary, linq, or whatever
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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
ByGoalZOPā€¢9mo ago
yea thats what I tried, how would you do it?
TheRanger
TheRangerā€¢9mo 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
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
No description
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
a very simple 1 liner
TheRanger
TheRangerā€¢9mo ago
I usually avoid spoonfeeding its not much of a challenge if someone does it for you
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
My bad, sorry I've just joined šŸ™‚
ByGoalZ
ByGoalZOPā€¢9mo 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ā€¢9mo ago
thats lambda expression you could just use a nested for loop
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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
ByGoalZOPā€¢9mo ago
How?
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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
ByGoalZOPā€¢9mo ago
Ok so: Iterate through the array -> then what? I cant just compare the current element to the next one right?
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
It also takes a lambda returning a bool, known in most languages as a predicate, a condition
ByGoalZ
ByGoalZOPā€¢9mo ago
I did it lol
No description
TheRanger
TheRangerā€¢9mo ago
You can start by wrapping your own Count method nvm then lol
ByGoalZ
ByGoalZOPā€¢9mo ago
Just did this
No description
TheRanger
TheRangerā€¢9mo ago
That's a linq method
ByGoalZ
ByGoalZOPā€¢9mo 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ā€¢9mo 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
ByGoalZOPā€¢9mo 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ā€¢9mo ago
but your elements arent all zero, so the variable will be false
ByGoalZ
ByGoalZOPā€¢9mo 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
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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ā€¢9mo ago
what do u mean by double int
ByGoalZ
ByGoalZOPā€¢9mo ago
poor choice of words but every digit that appears twice lol my code is a mess and overcomplicated ik šŸ˜‚
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
Beginning programming always is, don't worry šŸ™‚
ByGoalZ
ByGoalZOPā€¢9mo 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
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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
ByGoalZOPā€¢9mo ago
ooh thanks
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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
ByGoalZOPā€¢9mo ago
Thanks, this worked even better and way less code. Gonna try it with LINQ too.
No description
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
No worries, I sketched out something like this; if you wanted to compare
No description
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
If you're interested in what's likely, or similarly going on inside those LINQ Maybe not quite as naive, but effectively similar
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo ago
No description
ben SKVU4-9YBC
ben SKVU4-9YBCā€¢9mo 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