Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 09:53 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
Rep Power: 0
bugger is on a distinguished road
Default removing duplicates from arrays
I have a String array with values in it. Their are duplicates in it. I want to duplicates from the array but don't know how to do it. I understand that I can do this using for loop - but then I have to use nested for loops and I think its not a good way of doing it.

If someone knows a better way, please advice.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-13-2007, 01:02 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Use methods.
Code:
public class RemoveDuplicates {
    static String[] strs = {
        "arrays", "java", "manipulate", "java",
        "simple", "arrays", "manipulate", "java"
    };

    public static void main(String[] args) {
        print(strs, "strs");
        int num = getNumUniqueValues();
        String[] uniqueValues = new String[num];
        for(int j = 0, k = 0; j < strs.length; j++) {
            if(!containsValue(uniqueValues, strs[j]))
                uniqueValues[k++] = strs[j];
        }
        print(uniqueValues, "uniqueValues");
    }

    private static int getNumUniqueValues() {
        String[] values = new String[strs.length];
        int count = 0;
        for(int j = 0; j < strs.length; j++) {
            if(!containsValue(values, strs[j]))
                values[count++] = strs[j];
        }
        return count;
    }

    private static boolean containsValue(String[] array, String target) {
        for(int j = 0; j < array.length; j++) {
            if(array[j] != null && array[j].equals(target))
                return true;
        }
        return false;
    }

    private static void print(String[] array, String s) {
        System.out.println(s + ":");
        for(int j = 0; j < array.length; j++) {
            System.out.print(array[j]);
            if(j < array.length-1)
                System.out.print(", ");
        }
        System.out.println();
    }
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 10:11 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
Rep Power: 0
bugger is on a distinguished road
Default
Thanks hardwired. I appreciate this.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 07:11 PM
Member
 
Join Date: Nov 2007
Posts: 7
Rep Power: 0
Gambit17 is on a distinguished road
Default
yo bugger had a similar problem a week a ago if you want to know how i sorted it out just say so on this thread. yip struggled with it for awhile but gor it in the end
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
No duplicates allowed in Sets Java Tip Java Tips 0 01-21-2008 05:33 PM
image removing Triss New To Java 3 01-20-2008 09:27 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
Duplicates Gambit17 New To Java 5 11-08-2007 10:56 AM
duplicates in iReport Heather Advanced Java 1 07-05-2007 05:42 AM


All times are GMT +2. The time now is 12:15 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org