Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removing duplicates from an array

Posted on 2008-10-25
4
Medium Priority
?
1,661 Views
Last Modified: 2010-04-21
Hi guys,

I have a file from which I'm reading a Point objects.  I put them into an array, and I need to remove any duplicates.  I think my code should work, but when I check the elements in the array, duplicates are still there. Can you please provide some feedback on what I'm doing wrong?

p.s. The code to initially fill the array works, the only problem that I have is that the duplicates are not being removed. (Lines 8 to 10)
public void fillArray(){
			array = new Point[this.arraySize()];
                        String line = fileReader.readLine();
			for(int i=0; line!=null; i++){
				array[i] = this.convertPoint(line);
                                line = fileReader.readLine();
			}
			List<Point> list = Arrays.asList(array);
			Set<Point> set = new HashSet<Point>(list);
			array = (Point[])set.toArray(new Point[]{});
	}

Open in new window

0
Comment
Question by:ubuntuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22805908
The Point must be able to be compared if I am not mistaken as the Set/HashSet will not allow duplicates, but it must have some way to tell if one Point object is equal to another Point object in order to work.
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 800 total points
ID: 22805938
If it is true that lines that are equal create Points that are equal and different lines create different Points, then this should work.

(my code may not be all the way correct, just threw it together to give you the overall concept -- you should be able to use as a basis for correcting code)

good luck.


public void fillArray(){
    Set<String> set = new HashSet<String>();
    String line = fileReader.readLine();
    for(int i=0; line!=null; i++){
        set.add(line);
        line = fileReader.readLine();
    }
    
    List<Point> list = new List<Point>();
    Iterator<String> it = set.iterator();
    while (it.hasNext()) {
        list.add(this.convertPoint(it.next());
    }
    
    array = list.toArray(array);
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 1200 total points
ID: 22806041
i have written a simple example which will remove dupliactes.may be it will help you.

      String[] strArray = new String[]{"Object","Array","Converted","List","List"};
               
                        
                List list = Arrays.asList(strArray);
               
                  Set<Point> set = new HashSet<Point>(list);
                  strArray = (String[])set.toArray(new String[]{});
                  for (int i = 0; i < strArray.length; i++) {
                        System.out.println(strArray[i]);      
                  }
0
 
LVL 1

Author Closing Comment

by:ubuntuguy
ID: 31510033
Thanks guys, chaituu solution worked very well for strings, then I simply put the array of string into a new array converting each string into a point.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question