#Why does this keep returning true ?

1 messages · Page 1 of 1 (latest)

tidal berry
#

I’m trying to create a method that takes an array and checks if any elements are duplicates.

thick boltBOT
#

<@&987246883653156906> please have a look, thanks.

thick boltBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

inland river
#

and also tell us what ur input is that ur testing with

inland river
#

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"

tidal berry
#

OHHH

#

so there will be a dulicate everytime

#

duplicate

#

how do i make it not test its self?

inland river
#

on the example [1, 2, 3]. who do u want to compare against the 1?

#

right now u compare 1, 2, 3 against it

tidal berry
#

ur right

#

i want 2, 3 to compare

inland river
#

okay. next one

#

the 2

#

who to compare against?

tidal berry
#

1 and 3

inland river
#

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?

tidal berry
#

i wouldn't want the same element to be compared

inland river
#

and when is that the case?

#

(using i and j)

tidal berry
inland river
#

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?

tidal berry
#

sorry i dont understand the question

inland river
#

do u understand the snippet i posted? with the i and j

#

and how it represents ur code

tidal berry
#

yeah thats how the loop runs

inland river
#

perfect

#

so. when i is 1 and j is 2, which numbers does the algorithm compare currently?

#

4 against 8? (no)

tidal berry
#

8 and 2

inland river
#

yes

#

so. looking at the snippet, which comparisons are the ones we dont want?

#

its these:

tidal berry
#

oh i see the question

#

its 8 and 2

inland river
#
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

tidal berry
#

ahh that makes sense

inland river
#

now, look at the numbers. do u see a coincidence?

#

compare i with j

tidal berry
#

it's a guaranteed duplicate?

inland river
#

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

#

🙂

tidal berry
#

i do that very very often

inland river
#

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

tidal berry
#

ah yes ur right

inland river
#

so now u just have to tell ur code to skip/exclude this comparison

tidal berry
#

how do i skip the number tho

#

exactly

inland river
#

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

tidal berry
inland river
#
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++;
    }
  }
}
tidal berry
inland river
#

which part is unclear?

#

we collected everything we need already

tidal berry
#

the not self comparison

inland river
#

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

tidal berry
#

numbers[i] == numbers[j]

inland river
#

mh, seems we have to step back then

#

it didnt fully click yet

tidal berry
#

sorry man

inland river
#

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?

tidal berry
#

yes thats clear

inland river
#

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

tidal berry
#

yep

inland river
#

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

tidal berry
#

we just want to block first 8 == first 8 right

inland river
#

exactly

#

(and last 8 with last 8)

tidal berry
#

yup

inland river
#

looking at the table again, whats the correlation, it is:

#

so now i just color highlighted it

#

nothing else

tidal berry
#

yes and we dont want i and j to equal the same number?

inland river
#

exactly. thats the condition

tidal berry
#

becuase then it would be the same exact number nota dupe

inland river
#

a self comparison occurs when i and j are identical

#

how to write i and j are identical in code?

tidal berry
#

number[i] == number[j];

inland river
#

nope

tidal berry
#

i == j

inland river
#

are u guessing now?

#

😄

tidal berry
#

sorry im just getting frustrated

inland river
#

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)

tidal berry
inland river
#

right now we dont want to identify duplicates

tidal berry
#

becuase if two elements equal each other it would return true

inland river
#

we want to identify self-comparisons

tidal berry
#

oh i see

inland river
tidal berry
#

oh ok

inland river
#

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)

tidal berry
#

oh sorry i misunderstood you're questioning

inland river
#

ah, makes sense then cat_thumbs_up

#

so. to summarize:

  • do we have a duplicate? numbers[i] == numbers[j]
  • do we have a self-comparison? i == j
#

alright?

tidal berry
#

yes

inland river
#

nice

#

so lets get back to the code

tidal berry
#

that makes much more sense now

inland river
#
for (...) {
  for (...) {
    if (is duplicate && not self comparison) {
      count++;
    }
  }
}
#

plug and play

tidal berry
#

so not self comparison i != j?

inland river
#

yes exactly 🙂

#

and thats all u have to change in ur current code

tidal berry
#

ur an extremely good teacher man

inland river
#

thanks peepo_heart

#

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

tidal berry
inland river
#

can u reshare the code? 🙂

tidal berry
#

ok

#

i have to send a picture again tho because of circumstance

inland river
#

no worries

tidal berry
#

actually worked nvm

inland river
#

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

tidal berry
#

i can only use one return in the method

#

its a habit i have to learn for class

inland river
#

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

inland river
#

ur existing self-comparison thing

tidal berry
#

oh right

inland river
#

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

tidal berry
#

ah makes sense

inland river
#

(a much better approach would be to use Sets)

tidal berry
#

its extremely late where im at so i have to sleep but are u free tomorrow by any chance?

inland river
#

have a good one. i should be there tomorrow as well. otherwise, someone else will be able to assist as well 🙂

tidal berry
#

ah ok thank you have a great night