Connecting Tech Pros Worldwide Forums | Help | Site Map

Delete duplicates array (String)

Newbie
 
Join Date: Mar 2007
Posts: 11
#1: Aug 5 '07
Hello,


I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).


for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res[l].equals(my[k])){
isthere=true;
break;
}
}
if (isthere == false){
res[free]= my[k];
temp.append(res[free] + "\n");
free++;
}
}

The beanshell gives me an error in the method .equals(my[k]).

Has anyone work with beanshell already?
Does anyone has an idea how to delete duplicates from an array of String in a easier way?

I also thought about java.util.Set, but it not working too.

Thank you in advance,

Mokita

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 5 '07

re: Delete duplicates array (String)


Quote:

Originally Posted by Mokita

Hello,


I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).


for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res[l].equals(my[k])){
isthere=true;
break;
}
}
if (isthere == false){
res[free]= my[k];
temp.append(res[free] + "\n");
free++;
}
}

The beanshell gives me an error in the method .equals(my[k]).

Has anyone work with beanshell already?
Does anyone has an idea how to delete duplicates from an array of String in a easier way?

I also thought about java.util.Set, but it not working too.

Thank you in advance,

Mokita

BeanShell is great fun to work/play with; as far as I know you can't use generics
yet so you have to do this (for arrays of Objects of any kind)

Expand|Select|Wrap|Line Numbers
  1. Set set= new HashSet(Arrays.asList(yourArray));
  2.  
The set contains all your elements from yourArray but only once.

kind regards,

Jos
Newbie
 
Join Date: Mar 2007
Posts: 11
#3: Aug 5 '07

re: Delete duplicates array (String)


Quote:

Originally Posted by JosAH

BeanShell is great fun to work/play with; as far as I know you can't use generics
yet so you have to do this (for arrays of Objects of any kind)

Expand|Select|Wrap|Line Numbers
  1. Set set= new HashSet(Arrays.asList(yourArray));
  2.  
The set contains all your elements from yourArray but only once.

kind regards,

Jos


Thank you very much, it was very helpful.

Mokita
Newbie
 
Join Date: Mar 2008
Location: Bangalore
Posts: 1
#4: Mar 20 '08

re: Delete duplicates array (String)


Quote:

Originally Posted by Mokita

Hello,


I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).


for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res[l].equals(my[k])){
isthere=true;
break;
}
}
if (isthere == false){
res[free]= my[k];
temp.append(res[free] + "\n");
free++;
}
}

The beanshell gives me an error in the method .equals(my[k]).

Has anyone work with beanshell already?
Does anyone has an idea how to delete duplicates from an array of String in a easier way?

I also thought about java.util.Set, but it not working too.

Thank you in advance,

Mokita


Reply:-

HI,
Try the following logic for String array...........

private static void removeDuplicatesFromArray(){
int exampleArray [] = {4,5,1,1,2,3,2,4,5,1,2,0};// Example Array!
int exampleArrayLength = exampleArray.length;
int searchArray [] = new int[exampleArrayLength]; // Empty Array used to remove duplicates from Example Array
int searchArrayLength=1;

//I now want to print the above array without any duplicate values, which is my problem!
for(int x = 0; x < exampleArrayLength; x++){
if(x==0){
searchArray[0] = exampleArray[0];
}else{
int elementFound=0;
for(int y=0;y<searchArrayLength;y++){
if(exampleArray[x] != searchArray[y]){
elementFound=0;
}else{
elementFound=elementFound+1;
break;
}
}
if(elementFound==0){
searchArray[searchArrayLength] = exampleArray[x];
searchArrayLength++;
}
}
}

int outArray[]=new int[searchArrayLength];
System.arraycopy(searchArray,0,outArray,0,searchAr rayLength);

for(int x = 0; x < outArray.length; x++){
System.out.println(outArray[x]);
}
}

Regards,
Karthik.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Mar 20 '08

re: Delete duplicates array (String)


Quote:

Originally Posted by sriharis

Reply:-

HI,
Try the following logic for String array...........

Did you read reply #2? (eight months ago).

kind regards,

Jos
Reply