Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

Duplicate element in array of java

Duplicate element in array of java

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
juniormember



Joined: 28 Jun 2008
Posts: 154

PostPosted: Fri Aug 08, 2008 3:14 pm    Post subject: Duplicate element in array of java Reply with quote

If we need to remove and find duplicate array in java, what is possible way to do this. Finding duplicate array should be fast and accurate, because more element mean more big and slow search of duplicate array. After finding duplicate array next will be to remove array from element and maintain the correct order.

Array can be in ArrayList, Vector. Array can be treated as object or string or integer.

First sort value of array than one by one match next and previous element.
Back to top
View user's profile Send private message
strutschamp



Joined: 04 Mar 2007
Posts: 4

PostPosted: Wed Dec 24, 2008 1:22 pm    Post subject: Reply with quote

No direct way to find duplicate array in java. Java array provide no such method to find duplicate elements. Array allow us to duplicate elements. If donot want to insert duplicate element, should not use array. Use HashMap like collection.

Or build own logic to find duplicate in array. It is not easy process to make own function to find it. But still can make similar function to find duplicate
Back to top
View user's profile Send private message
seniormember
Site Admin


Joined: 20 Jul 2008
Posts: 77

PostPosted: Wed Apr 07, 2010 3:09 am    Post subject: Reply with quote

Removing duplicate object through collection framework is easy with set interface.

You can try this code to find duplicate array through collection

Quote:


ArrayList<String> arrayList1 = new ArrayList<String>();

arrayList1.add("A");
arrayList1.add("A");
arrayList1.add("B");
arrayList1.add("B");
arrayList1.add("B");
arrayList1.add("C");

for (int x=0; x< arrayList1.size(); x++)
{
System.out.println("arrayList1 :"+arrayList1.get(x));
}
Set s=new TreeSet();
s.addAll(arrayList1);
Iterator it=s.iterator();
while (it.hasNext())
{
System.out.println("Set :"+(String)it.next());
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack