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.

Showing Answers 1 - 7 of 7 Answers

indupriya

  • Apr 9th, 2006
 

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: }

  Was this answer useful?  Yes

Alex

  • Mar 2nd, 2007
 

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);

shriddhas

  • Aug 26th, 2009
 

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]);
  }
 }
}

  Was this answer useful?  Yes

ajithg4u

  • Aug 26th, 2009
 

Sort the array and compare the adjacent elements if the adjacent elements are equal then there will be duplicates.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions