PayPal Interview Question
Software Engineer / DevelopersConstruct a list
(see list::unique on cplusplus.com)
double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
list<double> mylist (mydoubles,mydoubles+10);
mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35
mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
// Arr = initial array, Len = initial array length, pRes = pointer to the result array, resLen = length of the result array
template <class T>
void RemoveDuplicatesFromArray(T Arr[], T Len, T*&pRes, T&ResLen)
{
int i;
bool bExists;
T* pCurrent;
if(Len==0)
{
pRes=NULL;
ResLen=0;
return;
}
pRes = new T[Len];
// Always put the first element
*pRes = Arr[0];
ResLen=1;
// then check the result array - do we already have such element?
for(i=1;i<Len;i++)
{
pCurrent = pRes;
bExists=false;
do
{
if(*pCurrent == Arr[i])
{
bExists=true; // found the same element in result array
break;
}
pCurrent++;
} while(pCurrent < pRes+ResLen);
if(bExists==false)
{
*pCurrent = Arr[i]; // add the element to the result array
ResLen++;
}
} // end of "for"
}
In an array of char:
std::string removeDuplicateChar(const std::string& str)
{
std::string result;
typedef std::bitset<__SCHAR_MAX__ * 2U + 1> Bitset;
Bitset bitset;
std::string::const_iterator it = str.begin(), end = str.end();
for (; it != end; ++it)
{
Bitset::reference ref = bitset[*it];
if (!ref)
{
result += *it;
}
ref = 1;
}
return result;
}
package com.linus.interview.algo;
import java.util.ArrayList;
import java.util.List;
public class RemoveDuplicates {
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = { 1, 3, 4, 1, 2, 6, 2, 3, 33, 11, 33, 4, 99 };
removeDuplicates(arr);
}
private static void removeDuplicates(int[] arr) {
int[] temp = new int[arr.length];
List<Integer> list = new ArrayList<Integer>();
int j=0;
for (int i = 0; i < arr.length; i++) {
if (list.contains(arr[i])) {
} else {
list.add(arr[i]);
temp[j]=arr[i];
j++;
}
}
arr=temp;
//this is only for printing
for(Integer i : arr){
System.out.println(i);
}
}
}
ArrayList is the solution. HashSet can be used too, but AL is faster a bit.
public static void main(String[] args) {
int[] arr = { 1, 3, 67, 3, 6, 88, 9, 56, 4, 5, 7, 23, 4, 99 };
removeDuplicatesUsingAL(arr);
// removeDuplicatesUsingHashSet(arr);
}
public static void removeDuplicatesUsingAL(int[] arr) {
long startTime = System.nanoTime();
System.out.println("Start time: " + startTime);
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (!list.contains(arr[i])) {
list.add(arr[i]);
}
}
for (Integer i : list) {
System.out.println("values are: " + i);
}
long endTime = System.nanoTime();
System.out.println("End time: " + endTime);
System.out.println("total Time is: " + (endTime - startTime));
}
public static void removeDuplicatesUsingHashSet(int[] arr) {
long startTime = System.nanoTime();
System.out.println("Start time: " + startTime);
Set hashSet = new HashSet();
for (int i = 0; i < arr.length; i++) {
if (!hashSet.contains(arr[i])) {
hashSet.add(arr[i]);
}
}
Iterator iter = hashSet.iterator();
while (iter.hasNext()) {
System.out.println("values are: " + iter.next());
}
long endTime = System.nanoTime();
System.out.println("End time: " + endTime);
System.out.println("total Time is: " + (endTime - startTime));
}
using Systems;
using Systems.Collections.Generic;
class sample
{
public static void Main()
{
int[] arr={1,2,3,1,2,3,1,4,5};
Dictionary<int,int> dict=new Dictionary<int,int>();
foreach(var a in arr)
{
if(!dict.ContainsKey(a))
dict.Add(a,1);
}
foreach(var a in dict)
{
Console.Write(a.Key+" ");
}
}
}
Put items in a hash table one by one. If any item repeats then dont put it. Give back the hash table. Its uniques
- sati June 30, 2011