Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check if all elements in an array are different with a boolean?  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to see if all the numbers in an array are distinct(ex:{1,2,3,4} would be true and {1,2,4,1} would be false. I have tried using a for loop to make it go through each number in the array and then use and if statement that if a[i] doesnt equal a[i+1] to return it as true, but for some reason everything seems to be returning back as true. What am I doing wrong here?

 
omar elgazzar
Greenhorn
Posts: 11
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code compares 2 adjacent elements, if they're not equal it return true (ends the method)
so, if we have an array {1, 2, 4, 1} it will test if 1 != 2, it's true so it return true
the solution is you should test if 1==2, if true return false, and at the end of the method 'return true' because we didn't find equal elements.
But
This is just comparing 2 adjacent elements 1==2?, 2==4?, 4==1? , It will definitely say they're all different, so one solution i can think of is a nested loop like this
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omar elgazzar wrote:so one solution i can think of is a nested loop like this...

Hi Omar, and welcome to JavaRanch. I gave you a tick for your explanation (which is correct), but in general we suggest that you don't give full solutions in code.

The reason is that we want people to think - especially in the Beginners Forum - and.If you simply hand someone ready-made code, they may just use it without thinking about it.

Your solution is also far from being optimal (although it will probably to do the trick). I'm sure your motives were the best, but next time provide "hints", rather than a complete solution.

Thanks.

Winston
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,
First of all you need to have 2 for loops in this case. Think it like you pick the first value from array (keep it fix) and compare it with all values(except itself); Then you pick the second value from the array (keep it fix)and compare it with other values and so on.So your first for loop will provide you first value and second for loop will provide second value to compare.
Your solution is checking for adjacent values so it will work only when there is confirmation that same values will always appear together. Can you think of a case where same values will appear together? Can you modify this array to become like that. If you can think of this then your solution will also valid and this will be second solution to this problem.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can sort the array. Although sorting will require a loop, there is now no need for a second loop for searching. What's more, you can sort with quicksort in nlogn time whereas your two loops will run in n² complexity.Remember that allDifferent is the generalisation of the conjunction of all the individual pairs, so one pair being the same will make allDifferent false. That particular version avoids multiple return.

Conjunction = logical AND.
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you really want a lazy solution, letting the comp do all the work,
(a being the int[])

Set<Integer> set = Arrays.stream(a).boxed... // et cetera
return the comparison of the sizes of set and a
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:. . . a lazy solution, letting the comp do all the work, . . .
That is not a lazy solution; it is normal to make the computer do all the work. Unfortunately a lot of the questions here on “beginning” are intended to make the students write the algorithm for themselves, so they would not like the solution with a Set.
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. All these students don't want to use a Set and a two line solution
without loops. I should have realized that. Mea culpa
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!