473,390 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Delete duplicates array (String)

11
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
Aug 5 '07 #1
4 13939
JosAH
11,448 Expert 8TB
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
Aug 5 '07 #2
Mokita
11
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
Aug 5 '07 #3
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.
Mar 20 '08 #4
JosAH
11,448 Expert 8TB
Reply:-

HI,
Try the following logic for String array...........
Did you read reply #2? (eight months ago).

kind regards,

Jos
Mar 20 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
8
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates...
1
by: Patrizio | last post by:
Hi All, I've the following table with a PK defined on an IDENTITY column (INSERT_SEQ): CREATE TABLE MYDATA ( MID NUMERIC(19,0) NOT NULL, MYVALUE FLOAT NOT NULL, TIMEKEY ...
3
by: Alexander Anderson | last post by:
I have a DELETE statement that deletes duplicate data from a table. It takes a long time to execute, so I thought I'd seek advice here. The structure of the table is little funny. The following is...
3
by: Quack Boy | last post by:
I'm new to MS Access (97) and I need for a query to find *near* matches (not exact duplicates) mainly to weed out duplicates that may contain similar erronious data. EG Table1 ...
3
by: _eddie_ | last post by:
I'm building an array of strings on the fly from a database. What is the best method for eliminating duplicates? (I can do this before or after the strings are added to the array)
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
2
by: farouqdin | last post by:
Hi all i have code which loops through table and deletes the duplicate records. This code does it for one table. How do i change it so it goes through several tables? On Error Resume Next Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.