fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static int[] duplicatesRemoval(int[] input){
  12.  
  13. int f = 0;
  14. int s = 1;
  15. //RETURN IF THE ARRAY LENGTH IS LESS THAN 2;
  16. if(input.length < 2){
  17. return input;
  18. }
  19. while(s < input.length){
  20. if(input[s] == input[f]){
  21. s++;//
  22. }else{
  23. /*
  24.   ** THIS PART CAN BE SHORTENED BY input[++f] = input[s++];
  25.   */
  26. f++;//SINCE LAST POSITION IS 1 BACK TO CURRENT ONE
  27. input[f] = input[s];
  28. s++;//SINCE S POSITION IS ALREADY TRAVERSED SO INCREMENT IT
  29. }
  30. }
  31. //reassigning to new array element
  32. int[] result = new int[f+1];
  33. for(int k=0; k<result.length; k++){
  34. result[k] = input[k];
  35. }
  36.  
  37. return result;
  38. }
  39.  
  40. public static void main(String a[]){
  41. int[] input = {2,3,6,6,8,9,10,10,10,12,12};
  42. int[] output = duplicatesRemoval(input);
  43. for(int i:output){
  44. System.out.print(i+" ");
  45. }
  46. }
  47.  
  48. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
2 3 6 8 9 10 12