4

I know how to find duplicate elements in array but recently in an interview question, I was asked to find duplicate elements in array in a single pass meaning without using nested loops and recursion. I tried but failed. The interviewer wasn't kind enough to even give me a hint. So I came here to ask, Is it possible to find duplicate elements in array without nested loops/recursion? If yes can anybody give an example code? Also Library functions are not allowed

P.S I would also like to know what impact does it have if we don't use loops or recursion. Is it related to complexity?

8
  • Should we use O(1) additional memory as well, or we have no such limitations? Commented Mar 9, 2016 at 11:13
  • If you're allowed to modify an array, you can use sorting. Commented Mar 9, 2016 at 12:08
  • 2
    @blazs You cannot sort an array in a single pass. Commented Mar 9, 2016 at 12:10
  • Ops, right. Well, it seems to me you should have asked the interviewer for details. Can you assume the elements are integers? If yes, then you can sort using counting sort. If not, then you can use hash table and have O(n) expected time. Etc. etc. Commented Mar 9, 2016 at 12:13
  • We don't know what the interviewer has really asked you, but as stated in your question, this is not possible. Commented Mar 9, 2016 at 12:17

5 Answers 5

4

You can keep a hash table/dictionary with item counts for each item value.

Sign up to request clarification or add additional context in comments.

8 Comments

@n.m. Why isn't coding your own hashtable allowed?
@Seb Try doing it without loops or recursion.
@n.m. Without using nested loops? Sure. Multiple loops can be merged into one using continue...
@n.m. You just changed the goalposts... First you were talking about the "loops and recursion" requirement, and now you've moved on to the other requirement. Did you realise you were wrong about that first one? That's okay, I'm happy to move on... You don't have enough information to extrapolate "find duplicate elements in array in a single pass" to "must not perform passes of other arrays".
@n.m. It's a loop that accesses every element precisely once, yes. Does that mean it can't loop multiple times per element access?
|
0

Since you did not mention anything about the array, I'd say it isn't possible to do it in single pass. I'm not sure if counting sort is the answer you're looking for. So, the only solution to your problem is to use a dictionary. This is the most simplest implementation of a dictionary I could find in C.

Quick Way to Implement Dictionary in C

Hope this helps.

Comments

0

Since you want to find duplicates in linear time, e.g. one pass, you must use an additional data structure to count the number of times each element appears.

Here I assume the array is of integers. This would work for any type though. I use a HashMap that uses the elements, in this case integers, as the key, and the number of occurrences as the value.

public static ArrayList<Integer> findDuplicates(int[] arr) {

   HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
   for (int i = 0; i < arr.length; i++) {
       if (!map.containsKey(arr[i]))
           map.put(arr[i], 1);
       else
           map.put(arr[i], map.get(arr[i]) + 1);
   }

   ArrayList<Integer> dups = new ArrayList<Integer>();

   for (Integer i : map.keySet())
       if (map.get(i) > 1)
            dups.add(i);

  return dups;
}

So

int arr[] = {1,2,3,4,2,1,2,3,4,5,6,7,8};
findDuplicates(arr);

would return [1, 2, 3, 4].

Comments

-1

Simple - but creative - solution: Propose the requirement the array should be sorted - Then its going to be trivial. (But that is how interview questions work)

7 Comments

I think we can safely assume the response would be "Nice try, but it's not"
I don't think so - Not where I'm interviewing. I like to hear creative answers rather than text book answers. The hash proposed in other answers is a perfect solution given the fictuous requirement but does not lead to a perfect program. And if you had a real case of finding duplicates, you could probably improve code quality a lot when asking for a sorted array - Which could, in reality, a piece of cake to fulfil.
I just consider it too trivial to be interesting if the array is sorted. Why even ask the question if that's the case? If given the problem, I would probably confirm it with "I assume the array is unsorted?", just to let the interviewer know I had thought of it, but I'd be pretty surprised if the answer was "No, it's sorted, next question". What's the point?
The point is: I want the candidate to come up with the simplest solution. Even if it requires discussion and maybe re-work in other places. Because that is what I would expect from an engineer working in my team. Hashing is a nice solution to the problem given the constraints, but If you'd do that in a real team you'd probably have to be fired....
If you're interviewing bright people, and the solution was "oh, sorry, I forgot to tell you the array is already sorted", they'd probably finding it insulting. I know I would. Any such condition should be part of the problem statement. It isn't something someone would fail to notice in a real problem.
|
-1

I am assuming size of integer is 2 bytes.

#define ARRAY_SIZE 10

int array[ARRAY_SIZE] = {2,3,1,5,1,6,7,7,8,1};

int duplicate[65535] = {0};

for(char i = 0;i< ARRAY_SIZE;i++)
{
  duplicate[array[i]]++;
  if(duplicate[array[i]] > 1)
  {
     printf(" %d is duplicate in array",array[i]);
  }  
}

Now, each index of duplicate array shows number of times a value is repeated in array.

8 Comments

(1) The assumption is not reasonable and (2) this is not an O(n) solution. See my explanation for the other bad answer as to why this is so wrong.
@TomKarzes I am working on 78K0R compiler where sizeof integer is 2 bytes.
Doesn't matter. The values could be doubles, or structs. Even if they aren't, suppose you have an array of 10 integers ranging in value from 1 to 32767. It's hardly an O(n) solution, is it? In any case, this clearly is not the desired answer to OP's problem.
@TomKarzes could you please elaborate why this is not O(n) solution?
Because n is the length of the array, not the number of integers the machine can represent. The run time should scale linearly with the length of the array. Most modern general purpose processors are in fact 64 bits these days, but even at 32 bits creating an array large enough to hold all of them is impractical. Further, why do you think restricting the values to very tiny integers is reasonable? It should work on doubles or other data types as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.