#Why does this keep returning true ?
1 messages · Page 1 of 1 (latest)
<@&987246883653156906> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
There could be multiple reasons why your code keeps returning true. Without seeing the actual code, it's difficult to pinpoint the exact issue. However, here are a few common mistakes that could lead to this behavior:
-
Incorrect logic in the method: Make sure you have implemented the correct logic to check for duplicates in the array. You may need to compare each element with every other element in the array to determine if there are any duplicates.
-
Incorrect comparison: Check if you are using the correct comparison operator (e.g., == or .equals()) when comparing elements in the array. Depending on your use case, you may need to use .equals() for objects or == for primitive types.
-
Incorrect array traversal: Ensure that you are traversing through all elements of the array and comparing them properly. You might be missing some elements or not comparing them correctly.
-
Array sorting issue: If your array is not sorted before checking for duplicates, it can lead to incorrect results. Make sure you sort the array before performing any duplicate checks.
-
Data type mismatch: Verify that you are using the correct data type for your array elements and comparisons. If there is a mismatch between data types, it can lead to unexpected results.
To provide more specific guidance, please share your code so that we can review it and help identify any issues causing this behavior.
can u share the code as copy-able text please
and also tell us what ur input is that ur testing with
ur approach (despite being very inefficient) would definitely work. so likely just a minor bug
ah nvm, i found the issue
lets take the example [1, 2, 3]
and walk through what ur code does
the outer loop starts
i = 0
the inner loop starts j = 0
numbers[i] == numbers[j]
what is compared here?
numbers[0] == numbers[0]
1 == 1
do u notice the mistake?
the role of the outer loop is to take a number candidate
and the inner loop is then supposed to check the array for duplicates on this candidate
but ur checking ALL numbers
including the candidate itself
so ur logic will always find duplicates, cause each number is "duplicate with itself"
OHHH
so there will be a dulicate everytime
duplicate
how do i make it not test its self?
lets step back from code first
on the example [1, 2, 3]. who do u want to compare against the 1?
right now u compare 1, 2, 3 against it
1 and 3
and the 3 against 1, 2. okay
so. u want to compare against everyone except the current number
in ur code, thats controlled with i and j
i is the candidate and j the other number u want to compare against
so whats the condition in which u would like to NOT compare them?
i wouldn't want the same element to be compared
numbers[i] == numbers[j]
almost
😄
that would also trigger on actual duplicates
for example in [1, 2, 1, 1]
lets look at an example. [4, 8, 2]. here is what i and j will be:
i = 0
j = 0
j = 1
j = 2
i = 1
j = 0
j = 1
j = 2
i = 2
j = 0
j = 1
j = 2
thats a rundown of ur algorithm on the example [4, 8, 2]
can u tell which lines correspond to which comparisons?
for example i = 1, j = 2, which comparison is that?
sorry i dont understand the question
do u understand the snippet i posted? with the i and j
and how it represents ur code
yeah thats how the loop runs
perfect
so. when i is 1 and j is 2, which numbers does the algorithm compare currently?
4 against 8? (no)
8 and 2
yes
so. looking at the snippet, which comparisons are the ones we dont want?
its these:
i = 0
j = 0 // <-- 4 with 4
j = 1
j = 2
i = 1
j = 0
j = 1 // <-- 8 with 8
j = 2
i = 2
j = 0
j = 1
j = 2 // <-- 2 with 2
so those are the ones we dont want
ahh that makes sense
it's a guaranteed duplicate?
yeah. but whats the condition on these cases
how can we identify these cases based on i and j
ur previous attempt was to say numbers[i] == numbers[j]
but thats the number itself
like 4, 8, 2
so it would trigger on actual duplicates as well
but we only want to skip self-comparisons
so how can we identify a self-comparison?
its simple, perhaps ur overcomplicating/overthinking
🙂
i do that very very often
no worries
its when i and j are the same
ur comparing the number to itself when i == j
think about it, i and j are indices / positions
ur comparing numbers on position i with position j
so if position 5 is compared with position 5, u just compared the number to itself literally
ah yes ur right
so now u just have to tell ur code to skip/exclude this comparison
there are various ways
one would be to add it to ur condition
like adding to ur existing if sth like if (... && not a self comparison)
or u make use of the continue statement
continue will skip the current iteration of a loop
could you elaborate on this
for (...) {
for (...) {
if (self comparison) {
continue;
}
if (is duplicate) {
count++;
}
}
}
so thats one option
and the other is
for (...) {
for (...) {
if (is duplicate && not self comparison) {
count++;
}
}
}
how would i write out that
the not self comparison
how to write "self comparison"?
thats what we just figured out a minute ago
whats the condition to identify a self comparison
i.e. what we wanted to skip
numbers[i] == numbers[j]
sorry man
lets look at the example [8, 4, 8]
and let me quickly write out the full comparison table:
i = 0 // 8
j = 0 // 8 with 8
j = 1 // 8 with 4
j = 2 // 8 with 8
i = 1 // 4
j = 0 // 4 with 8
j = 1 // 4 with 4
j = 2 // 4 with 8
i = 2 // 8
j = 0 // 8 with 8
j = 1 // 8 with 4
j = 2 // 8 with 8
before we continue, is that table fully clear?
yes thats clear
okay. now lets mark the self comparisons
i = 0 // 8
j = 0 // 8 with 8 <- self
j = 1 // 8 with 4
j = 2 // 8 with 8
i = 1 // 4
j = 0 // 4 with 8
j = 1 // 4 with 4 <- self
j = 2 // 4 with 8
i = 2 // 8
j = 0 // 8 with 8
j = 1 // 8 with 4
j = 2 // 8 with 8 <- self
its those 3
yep
now, if u use numbers[i] == numbers[j] to identify self-comparisons, u would identify these instead:
i = 0 // 8
j = 0 // 8 with 8 <- blocked
j = 1 // 8 with 4
j = 2 // 8 with 8 <- blocked
i = 1 // 4
j = 0 // 4 with 8
j = 1 // 4 with 4 <- blocked
j = 2 // 4 with 8
i = 2 // 8
j = 0 // 8 with 8 <- blocked
j = 1 // 8 with 4
j = 2 // 8 with 8 <- blocked
5 instead of 3 comparisons
because in [8, 4, 8] the first and last 8 are different numbers
u dont want to block the comparison first 8 with last 8
because this is an actual duplicate
and not a self-comparison
we just want to block first 8 == first 8 right
yup
looking at the table again, whats the correlation, it is:
so now i just color highlighted it
nothing else
yes and we dont want i and j to equal the same number?
exactly. thats the condition
becuase then it would be the same exact number nota dupe
a self comparison occurs when i and j are identical
how to write i and j are identical in code?
number[i] == number[j];
nope
i == j
sorry im just getting frustrated
please take a look at the picture with the colors again
i = 0 and j = 2
thats the first 8 with last 8 comparison
if we use numbers[i] == numbers[j], we get:
numbers[0] == numbers[2]
8 == 8
true
but we wanted false here. cause first 8 with last 8 is NOT a self comparison
u gotta understand the difference between the index i/j (0, 1, 2) and the numbers they represent in the array (8, 4, 8)
i thought we would want true here thats why i was confused
right now we dont want to identify duplicates
becuase if two elements equal each other it would return true
we want to identify self-comparisons
oh i see
yeah, thats what u want ur method to do. but we are not there yet
oh ok
u got that part right already
thats indeed how to identify a duplicate
but we have to put in an extra condition for skipping self-comparisons
so we are now trying to figure out the condition to identify a self-comparison
(and just that)
oh sorry i misunderstood you're questioning
ah, makes sense then 
so. to summarize:
- do we have a duplicate?
numbers[i] == numbers[j] - do we have a self-comparison?
i == j
alright?
yes
that makes much more sense now
for (...) {
for (...) {
if (is duplicate && not self comparison) {
count++;
}
}
}
plug and play
so not self comparison i != j?
ur an extremely good teacher man
thanks 
there are more improvements that could be done to the code, but it should be working now
for example, when u have [x, y, z], u will eventually compare x with z and later z with x
that second comparison is technically obsolete
since u already compared that pair (the other way around)
and that can be achieved by starting j after i
very simple trick
for (int j = i + 1; ...)
and fun fact: this automatically gets rid of self-comparisons
so u wouldnt need that extra check anymore either
still doesn't work :/
can u reshare the code? 🙂
no worries
another remark. ur current code computes the amount of duplicates. even though its only interested in whether theres any duplicate.
so if ur array is very big (lets say 10 million) and the duplicate is perhaps already right at the beginning, ur code could already abort early on. but right now it computes the full thing instead
so instead of computing count, u could end the method right on the first duplicate
u can do that like:
for (...) {
for (...) {
if (numbers[i] == numbers[j] && ...) {
return true;
}
}
}
a return immediately ends the method
interesting. then u could theoretically do it like that:
u can do that like:
boolean isDuplicate = false;
for (...) {
for (...) {
if (numbers[i] == numbers[j] && ...) {
isDuplicate = true;
break;
}
}
}
return isDuplicate;
break ends the loop
oh, actually, we have a nested loop here
so we would need break-labels
u can do that like:
boolean isDuplicate = false;
Outer:
for (...) {
Inner:
for (...) {
if (numbers[i] == numbers[j] && ...) {
isDuplicate = true;
break Outer;
}
}
}
return isDuplicate;
looks weird though, ngl 😄
anyways, just some remarks so that u learn sth
what comes after &&
ur existing self-comparison thing
oh right
unless u use the trick with starting j after i
then its just:
boolean isDuplicate = false;
Outer:
for (...) {
Inner:
for (int j = i + 1; ...) {
if (numbers[i] == numbers[j]) {
isDuplicate = true;
break Outer;
}
}
}
return isDuplicate;
this code is now fairly optimal if we still want to keep the approach of iterating arrays
ah makes sense
(a much better approach would be to use Sets)
its extremely late where im at so i have to sleep but are u free tomorrow by any chance?
have a good one. i should be there tomorrow as well. otherwise, someone else will be able to assist as well 🙂
ah ok thank you have a great night