• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting duplicate element in an array  RSS feed

 
david colais
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys...
Can anyone suggest any method to remove duplicate entries in an integer array without using collection classes.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david colais wrote:Hi guys...
Can anyone suggest any method to remove duplicate entries in an integer array without using collection classes.


You can keep swapping the duplicate elements to the end, keep a count of the number of duplicates obtained. So the actualLenghtOfArray-numOfDuplicates would be the array with unique values. There might be few modifications/checks that would come into this as I havent tried this algorithm not sure if its the exact one. May be others can comment on this/correct this.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get the each index element and walk through the entire array each time if you find the identical entry just make the index to null
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
david colais wrote:without using collection classes.

Looks like homework. so what is your approach?
 
Venkat basa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take an array,

take for loop,create temp array,
if you want to remove {1,2,3,4,5} 3 element from it
for(code)
{
if(array[i]!=3)
{
sop(array[i]);
}
}

this way you can remove the any element from given array

venkat
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question - since the size of an array is fixed, what do you mean by "remove"? Do you mean set duplicate slots to null? Move everything after duplicates up one (possibly setting the last element to null)? Or creating a new, shorter array that contains everything except the duplicates?

(I can only assume this is an exercise, because in real code anyone in their right mind would use a collection class!)
 
Venkat basa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can take as int[] temp=new int[array.length-1] and, the question is without using Collections api..

so this my thought

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be better to follow the original suggestion of swapping duplicates to the end of the array. That reduces the overhead of repeatedly creating new arrays 1 smaller than previously.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!