Results 1 to 6 of 6

Thread: Duplicates

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

    Default Duplicates

    hey,

    How do you remove duplicates in an array and the shift the whole array down so you dont have empty blanks in your array?
    Thanks

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

    Default

    Java Code:
    import java.util.Random;
    
    public class ArrayResizing {
        public static void main(String[] args) {
            Random seed = new Random();
            int[] domain = new int[12];
            for(int j = 0; j < domain.length; j++)
                domain[j] = seed.nextInt(101);
            print(domain, "initial");
            for(int j = 0; j < 3; j++) {
                int index = seed.nextInt(domain.length);
                domain = removeElement(domain, index);
                print(domain, "remove element at " + index);
            }
        }
    
        private static int[] removeElement(int[] array, int index) {
            int n = array.length;
            int[] smaller = new int[n-1];
            for(int j = 0, k = 0; j < n; j++) {
                if(j == index)  // skip element at index
                    continue;
                smaller[k++] = array[j];
            }
            return smaller;
        }
    
        private static void print(int[] 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
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    You can traverse your array and recreate it. But temporarily you can keep the accepted elements inside a dynamic data structure like Vector since you will not know the size of the array until traversing is finished.

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

    Default

    If you're array contains non-primitive data, try adding the elements using Set...

    for example:

    import java.util.*;

    Java Code:
    public class Duplicate {
    
    	public static void main(String[] args) {
    		String[] s = {"1","2","2","2","2","3","3","4"};
    		s=removeDups(s);
    		System.out.println(Arrays.asList(s));
    	}
    	
    	public static String[] removeDups(String[] s){
    		Set unique = new HashSet();
    		for(int i=0; i<s.length; i++) unique.add(s[i]);
    		System.out.println(unique);
    		return (String[])unique.toArray(new String[0]);
    	}
    }

    hope this one gives you an idea too. THanks =)

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

    Default

    Thanks guys after like 5 hours i managed to do it like so:

    Java Code:
    	void removeDuplicates()
    	{
    		String[][] dupTemp = new String[100][4];
    		for(int a = 1; a < songNum;a++)
    		{
    			for(int b = a + 1; b < songNum + 1; b++)
    			{
    				if(song[a][0].equals(song[b][0]) && song[a][1].equals(song[b][1]) && song[a][2].equals(song[b][2]) && song[a][3].equals(song[b][3]))
    				{
    						song[b][0]=" ";
    						song[b][1]=" ";
    						song[b][2]=" ";
    						song[b][3]=" ";					
    				}
    			}//for b
    		}//for a
    		int size=0;
    		for(int i = 1; i <= songNum; i++)
    		{
    			if(song[i][0].equals(" ")) size++;
    			else dupTemp[i - size]=song[i];
    		}//for						
    		songNum -= size;
    		for(int i = 1; i <= songNum ; i++) song[i] = dupTemp[i];	
    	}//duplicates
    ya that works, do you know of an easier way tho

  6. #6
    bar311 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    public class Duplicate {
    
    	public static void main(String[] args) {
    		String[] s = {"2","2","4","1","2","2","3","3"};
    		s=removeDups(s);
    		System.out.println(Arrays.asList(s));
    	}
    	
    	public static String[] removeDups(String[] s){
    		List unique = new ArrayList();
    		for(int i=0; i<s.length; i++){
    			if(!unique.contains(s[i]))unique.add(s[i]);
    		}
    		return (String[])unique.toArray(new String[0]);
    	}
    }

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. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM
  3. 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
  •