Results 1 to 4 of 4
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Use methods.
    Java 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();
        }
    }

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks hardwired. I appreciate this.

  4. #4
    Gambit17 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    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

Similar Threads

  1. No duplicates allowed in Sets
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:33 PM
  2. image removing
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-20-2008, 08:27 PM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM
  4. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM
  5. duplicates in iReport
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •