Delete duplicates array (String)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mokita
    New Member
    • Mar 2007
    • 11

    #1

    Delete duplicates array (String)

    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
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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)

    [code=java]
    Set set= new HashSet(Arrays. asList(yourArra y));
    [/code]

    The set contains all your elements from yourArray but only once.

    kind regards,

    Jos

    Comment

    • Mokita
      New Member
      • Mar 2007
      • 11

      #3
      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)

      [code=java]
      Set set= new HashSet(Arrays. asList(yourArra y));
      [/code]

      The set contains all your elements from yourArray but only once.

      kind regards,

      Jos

      Thank you very much, it was very helpful.

      Mokita

      Comment

      • sriharis
        New Member
        • Mar 2008
        • 1

        #4
        Reply...for removing duplicates in a array without collections in java

        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 removeDuplicate sFromArray(){
        int exampleArray [] = {4,5,1,1,2,3,2, 4,5,1,2,0};// Example Array!
        int exampleArrayLen gth = exampleArray.le ngth;
        int searchArray [] = new int[exampleArrayLen gth]; // Empty Array used to remove duplicates from Example Array
        int searchArrayLeng th=1;

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

        int outArray[]=new int[searchArrayLeng th];
        System.arraycop y(searchArray,0 ,outArray,0,sea rchArrayLength) ;

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

        Regards,
        Karthik.
        Last edited by sriharis; Mar 20 '08, 06:51 AM. Reason: to edit the from

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by sriharis
          Reply:-

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

          kind regards,

          Jos

          Comment

          Working...