top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Efficient way to remove the duplicate in an array?

+5 votes
494 views

Input: {5, 8, 5, 3, 3, 2, 7}
Output: {5, 8, 3, 2, 7, ?, ?}

Idea is that we should remove the duplicate number from the array and should return the array in the original form (obviously without duplicate).

Please help me with an efficient algorithm or C code.

posted Mar 1, 2014 by Jai Prakash

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

A good efficient hash would probably provide the best solution.

for each "element" in "input"
{
      if { not found in "hash" }
      {
             insert in "output"
             insert in "hash"
      }
}

If you know the characteristics of input array elements, then you can have much efficient solution. For example, if input array is made of characters, then your "hash" can be just 4 integer numbers on a 32-bit operating system. Each bit on the int's would represent presence/absence of the ASCII value of character. You initialize all 4 ints as all 0's and turn the bit on when you encounter a given character. So, your entire "hash" is made of 4 integers.

answer Mar 2, 2014 by Mehul Bhatt
Similar Questions
+2 votes

Given an infinite supply of coins of denominations {20,10,5,1}, find out total number of way to make change of given amount - 'n'?
For example, if given amount is 20, there are 10 ways to make this change as shown below -
(1 of 20),(1 of 10 + 2 of 5),(1 of 10 + 1 of 5 + 5 of 1),(1 of 10 + 10 of 1), (2 of 10), (1 of 5 + 15 of 1),(2 of 5 + 10 of 1),(3 of 5 + 5 of 1),(4 of 5),(20 of 1)

If the amount given is 0 then the total number of ways to make change is 1 - using 0 coins of every given denomination.

...