• 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

Finding duplicate elements in an array without using nested loops

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently had an interview which consisted of following problem. Please help with possible solutions.

Write a function in Java to find duplicate elements in an integer array without using nested loops ( for/ while / do while, etc ) and without using library functions.
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First of all, in Java there are no functions. More correctly would be to ask to write a program or method.

I'd say you could solve that with recursion if there's no restriction to use it.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would a Stream be permissible?

Remember in an interview it is not what you say so much as how you say it. The way you approach the question and the sort of questions you ask for clarification count as much as the actual solution you produce. Whether saying the producing such code in real life merits instant dismissal is a good idea, I am not sure.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it says no NESTED loops, right?

use a loop, and use each element as a key to map. for each one, first check to see if the key exists - if so, you have a duplicate. if not, insert it.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two sets are used, and looking at the return type when you insert
an element into a set, the solution can be very short. As Fred says,
you need just one loop.

Worrying though, is this requirement:

Dev Choudhary wrote:(...) and without using library functions.


Whatever that means...

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic