Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 09:45 AM
Member
 
Join Date: Nov 2007
Posts: 7
Gambit17 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 10:49 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 10:51 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 11:39 AM
Member
 
Join Date: Nov 2007
Posts: 7
bar311 is on a distinguished road
If you're array contains non-primitive data, try adding the elements using Set...

for example:

import java.util.*;

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 =)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-08-2007, 11:47 AM
Member
 
Join Date: Nov 2007
Posts: 7
Gambit17 is on a distinguished road
Thanks guys after like 5 hours i managed to do it like so:

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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-08-2007, 11:56 AM
Member
 
Join Date: Nov 2007
Posts: 7
bar311 is on a distinguished road
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]); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 06:33 PM
removing duplicates from arrays bugger New To Java 3 11-13-2007 08:11 PM
duplicates in iReport Heather Advanced Java 1 07-05-2007 06:42 AM


All times are GMT +3. The time now is 09:36 PM.


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