• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find difference in 2 arrays ?

 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Has anyone faced problem like below

We have 2 arrays one is having size, say A having 0 to 98 .

Second array B has numbers from 1 to 100 that means it has 100 elements .

So in A we can put 99 values . Now we need to put element randomly from B to A . So on completion of array A , what element is remaining in array B that I need to find out .

This can be easily done using loops , but interviewer asked for another approach as he dont want to use Loops .

I found another approch like convert both arrays into Collection class like arraylist and use removeAll() method of the collection , but i checked internal implementation of the method which again using the loop.


Please give your suggestions ..


Thanks in advance ..


- Chetan
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chetan Dorle wrote:but i checked internal implementation of the method which again using the loop.


Was his requirement that you cannot use a loop in your own code, or is no looping anywhere allowed, not even in the standard API methods that you're going to call?

Because however you solve this, you're not going to escape having a loop somewhere, whether it's in your own code or in code somewhere in the standard API.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for your reply .

He is expecting not to have for loop in our code implementation .

Might be he is OK with API methods , can't gurantee . Still if there is any way to find difference in arrays then can suggest .

Thanks in advance .

- Chetan

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If array is sorted you can do it without looping (binary search). However, you need to loop to sort it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chetan Dorle wrote:but i checked internal implementation of the method which again using the loop.


Tip: never, EVER, check the internal implementation of a method, as it may vary from release to release.

So, unless your interviewer is a complete prat, he's trying to find out if you can solve the problem without writing a loop; and your solution looks as good as any.

Winston
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Winston and Rob for your reply ...

-Chetan
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Chetan Dorle wrote:but i checked internal implementation of the method which again using the loop.


Tip: never, EVER, check the internal implementation of a method, as it may vary from release to release.


Hmmm, sounds pretty extreme. Here are some good potential reasons to look at implementations, in my opinion:

* The documentation of a method may be poor or even nonexistent.
* There may be a bug in an implementation, and you need to understand how or why the method is deviating from its documented behavior.
* You may want to learn how they accomplished something you don't know how to do yourself.
* You may be modifying the code yourself as part of your work.

I would say, if you do look at a method implementation, be aware that there's a difference between the current internal implementation, which may change in future releases without warning, and the documented behavior of a method. The latter may also change in some cases, but that's much less common, especially for a widely-used library from a responsible third party. When possible, use methods based on what you know about them from their documentation, rather than their internal implementation. But do not, under any circumstances, let someone tell you to NEVER look at internal implementations.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also possible to take "without looping" to an extreme: you could use recursion in place of a loop. E.g. to print all elements in a List:

You could conceivably use tricks like this to avoid any looping at all, technically. It's possible, though unlikely, that this is what your interviewer wanted. This particular trick is of little practical value, other than as exercises in creative thinking maybe. Or practice in learning functional programming techniques. But there's no good reason in the real world to do this rather than simply using a loop, either a loop you write yourself, or one embedded in a library somewhere.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I would say, if you do look at a method implementation, be aware that there's a difference between the current internal implementation, which may change in future releases without warning, and the documented behavior of a method. The latter may also change in some cases, but that's much less common, especially for a widely-used library from a responsible third party. When possible, use methods based on what you know about them from their documentation, rather than their internal implementation. But do not, under any circumstances, let someone tell you to NEVER look at internal implementations.


True. I'm violating my own mantra of "never say NEVER"...

However, as a general principle, you should never (oops, that word again) write logic that relies on the implementation of a method in another class (and probably even in your own) - and that's precisely what Chetan was doing. Otherwise, what's the point of using an OO language?

And of your 4 points, I'm not sure I agree with the first. If a method is so badly documented that you feel you have no choice but to check its implementation, I'd be tempted to roll my own (maybe along with a stiff e-mail to the author); if it has NO documentation, I wouldn't touch it with a barge-pole. And even if you do, you should only check the code to see if you can work out its intent.

The other 3: absolutely.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic