“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
//takes array of elements and size of the array as inputs
//returns new array with dups removed and the newSize of //the ayyay
int compact(int n,arrar a)
{
int curr = 0;
int next=0;
int newSize = n;
How good would this be?
As you traverse through the elements in the list, form a binary search tree out of the elements in the list. As you go to each new element, search for the element in the tree. If found, proceed to the next element in the list. Else, insert the element into the tree before moving to the next element.
void main(void)
{
int const arraySize = 21;
int arr[arraySize] = { 1, 2, 4, 4, 4,4,4,4,4,7, 8, 9,14,14,14,14,14,14, 14, 14 ,40};
int current = 0;
int last = 1;
The code published earlier fails when the repeating elements of the array are not adjacent to each other or are scattered across the array.
In such a case the possible solution is do in place sorting of the array and ignore the adjacent repeating elements.
Here’s the code:-
#include
#include
void insort(int *,int);
int main(void)
{
int a[10]={20,7,4,2,8,9,14,4,14,1};
int i;
insort(a,10);
for (i=0;i=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i = i-1;
}
arr[i+1] = key;
}
}
Submitted By: Anand (pythonhacker) — February 5, 2008
A binary search tree can be used here. Extract every element from the array and insert into a BST if not already there.
Another option is to again use the excellent bitmap data structure. Here is the algorithm.
1. Initialize a bitmap data structure to maximum value in the array (last entry) + 1. Set all entries to zero.
2. Loop through the array. Turn on the bit at the index of the array element to 1.
3. Loop through the bitmap and print out all the bit indices which are 1. There you are.
Here is the code.
def nodups(l):
newl = []
# Using a bitmap
bitmap = [0]*(l[-1]+1)
for num in l:
bitmap[num] = 1
for x in range(len(bitmap)):
if bitmap[x]==1:
newl.append(x)
#include <stdio.h>
int main()
{
int a[10]={1, 2, 4, 4, 7, 8, 9, 14, 14, 20};
int i;
for (i = 0;i<10;i++)
{
if (a[i] != a[i+1])
printf(”%dn”,a[i]);
}
return 0;
}
#include <stdio.h>
main()
{
int a[10] = {0,4,4,4,5,6,6,8,9,9};
int i;
for(i=0; i<10; i++)
{
if(a[i] == a[i+1] )
{
while(a[i] == a[i+1]) i++;
}
else
printf(”%d “, a[i]);
}
printf(”n”);
}
Guys, you both will get an error when i = 9
if(a[9] == a[10]) ….
Guys look at this solution:
int arr[22] = new int[] { 1, 2, 4, 4, 4,4,4,4,4,7, 8, 9,14,14,14,14,14,14, 14, 14 ,40};
int i;
for(i=0;i
oid YankDup(int a[],int len)
{
int i;
printf(”(%d, “,a[0]);
for (i = 1; i
You’re all kinda wrong.
Don’t you consider the case when lenght(a)=1?
//takes array of elements and size of the array as inputs
//returns new array with dups removed and the newSize of //the ayyay
int compact(int n,arrar a)
{
int curr = 0;
int next=0;
int newSize = n;
next = curr+1;
while(next
How good would this be?
As you traverse through the elements in the list, form a binary search tree out of the elements in the list. As you go to each new element, search for the element in the tree. If found, proceed to the next element in the list. Else, insert the element into the tree before moving to the next element.
Oh sorry, that approach wouldnt be much efficient. We should be making use of the fact that the list at hand is sorted.
1,3,3,5,7,7,7,8,8,9
1,3,5,7,8,9
int *newlist(int *arr,int size)
{
int upper,lower=0;
for(int upper=1;upper
void remove_dup(list &l)
{
if ( l.size() > 1 ) {
list::iterator first = l.begin();
++first;
for ( list::iterator second = l.begin();
first != l.end(); ) {
if ( *second == *first ) {
first = l.erase( first );
}
else {
++first;
++second;
}
}
}
}
void main(void)
{
int const arraySize = 21;
int arr[arraySize] = { 1, 2, 4, 4, 4,4,4,4,4,7, 8, 9,14,14,14,14,14,14, 14, 14 ,40};
int current = 0;
int last = 1;
while (last
int a[] = { 1,2,3,3,4,5,5,5,6,6,7,7,7,7,8,10,10,11,12,15,16,17,17,20};
int current = a[0];
bool dup = false;
int size = sizeof(a) / sizeof(int);
for(int i = 0; i
The code published earlier fails when the repeating elements of the array are not adjacent to each other or are scattered across the array.
In such a case the possible solution is do in place sorting of the array and ignore the adjacent repeating elements.
Here’s the code:-
#include
#include
void insort(int *,int);
int main(void)
{
int a[10]={20,7,4,2,8,9,14,4,14,1};
int i;
insort(a,10);
for (i=0;i=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i = i-1;
}
arr[i+1] = key;
}
}
A binary search tree can be used here. Extract every element from the array and insert into a BST if not already there.
Another option is to again use the excellent bitmap data structure. Here is the algorithm.
1. Initialize a bitmap data structure to maximum value in the array (last entry) + 1. Set all entries to zero.
2. Loop through the array. Turn on the bit at the index of the array element to 1.
3. Loop through the bitmap and print out all the bit indices which are 1. There you are.
Here is the code.
def nodups(l):
newl = []
# Using a bitmap
bitmap = [0]*(l[-1]+1)
for num in l:
bitmap[num] = 1
for x in range(len(bitmap)):
if bitmap[x]==1:
newl.append(x)
return newl
print nodups([1,2,3,3,4,5,5,5,6,6,7,7,7,7,8,10,10,11,12,15,16,17,17,20])
#include
/* Here is my code */
int main()
{
int a[10] = {1,2,2,3,4,5,5,6,7,7};
int i,t;
int b[10];
t = a[0];
printf(”%d\t”,t);
for ( i = 1;i
Leave an Answer/Comment