GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Placement Papers  >  Computer Associates  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 22 of 23    Print  
Write a program to sort an given array and remove duplicates in it with out using java API and print the sort order and the number of occurrence of duplicates.

  
Total Answers and Comments: 4 Last Update: August 28, 2009     Asked by: krishna 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Alex
 
String names[] = {"Peter", "Patricia", "Hunter", "Sarah","Gabe", "Gina", "Rob", "John", "Zoey", "Tammy", "Robert","Sean", "Paschal", "Kathy", "Neleh", "Vecepia", "Robert"};
TreeSet set = new TreeSet(Arrays.asList(names));
for (String name : set)
System.out.println(name);

Above answer was rated as good by the following members:
shriddhas
April 09, 2006 06:08:48   #1  
indupriya        

RE: Write a program to sort an given array and remove ...
program for above question import java.util.*; 2: 3: class Name { 4: public static void main(String[] arguments) { 5: String names[] { Peter Patricia Hunter Sarah 6: Gabe Gina Rob John Zoey Tammy Robert 7: Sean Paschal Kathy Neleh Vecepia }; 8: System.out.println( The original order: ); 9: for (int i 0; i < names.length; i++)10: System.out.println(i + : + names[i]);11: Arrays.sort(names);12: System.out.println( The new order: );13: for (int i 0; i < names.length; i++)14: System.out.println(i + : + names[i]);15: }16: }
 
Is this answer useful? Yes | No
March 02, 2007 04:48:11   #2  
Alex        

RE: Write a program to sort an given array and remove ...
String names[] { Peter Patricia Hunter Sarah Gabe Gina Rob John Zoey Tammy Robert Sean Paschal Kathy Neleh Vecepia Robert };
TreeSet set new TreeSet(Arrays.asList(names));
for (String name : set)
System.out.println(name);

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 26, 2009 05:46:47   #3  
shriddhas Member Since: September 2008   Contribution: 1    

RE: Write a program to sort an given array and remove duplicates in it with out using java API and print the sort order and the number of occurrence of duplicates.

import java.util.Arrays;

class TestArray{
public static void main(String args[]){
int arr[] new int[]{2 5 1 1 2 3 4 3 4 4 5 5 15 35 5 5 45 5 5 5 65 5 5 5 5 5 5 6 17 7 7};
Arrays.sort(arr);

int newArray[] new int[arr.length];

int temp 0;
int count 0;

for(int i 0; i<arr.length; i++){
temp arr[i];
int val i+1;
if(val < arr.length){
if(temp arr[val])
continue;
}
newArray[count++] temp;
}


for(int i 0; i<newArray.length; i++){
System.out.println(newArray[i]);
}
}
}


 
Is this answer useful? Yes | No
August 26, 2009 21:52:39   #4  
ajithg4u Member Since: July 2009   Contribution: 2    

RE: Write a program to sort an given array and remove duplicates in it with out using java API and print the sort order and the number of occurrence of duplicates.
Sort the array and compare the adjacent elements if the adjacent elements are equal then there will be duplicates.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
Related Categories
Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape