http://www.technicalpage.net/search/label/SQL

uniqueString

 Get unique chars only in a string, ie, remove duplicate chars from a string.

 

package Test;

 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

 

public class GetUniqueCharsOnlyInAString {

 

       public static void main(String[] args) {

            

             Scanner scnr = new Scanner(System.in);

             System.out.println("Enter the string ");

             String word = scnr.nextLine();

            

             //String word =  "asidnsesassddienlsnemzeqz";

             String[] words = new String[word.length()];

             for(int i=0;i<word.length();i++) {

                    words[i]= Character.toString(word.charAt(i));

                   

             }

             System.out.println("array before sorting is "+Arrays.toString(words));//[a, s, i, d, n, s, e, s, a, s, s, d, d, i, e, n, l, s, n, e, m, z, e, q, z]

             Arrays.sort(words);

             System.out.println("array after sorting is "+Arrays.toString(words));//[a, a, d, d, d, e, e, e, e, i, i, l, m, n, n, n, q, s, s, s, s, s, s, z,z]

            

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

             for(int i=0;i<word.length()-1;i++) {

                    if(!(words[i].equals(words[i+1]))) {

                           alist.add(words[i]);

                    }

             }

             alist.add(words[word.length()-1]);// this is important

             System.out.println("Final Array is "+alist);

       }

 

}

 

Output:

Enter the string

asidnsesassddienlsnemzeqz

array before sorting is [a, s, i, d, n, s, e, s, a, s, s, d, d, i, e, n, l, s, n, e, m, z, e, q, z]

array after sorting is [a, a, d, d, d, e, e, e, e, i, i, l, m, n, n, n, q, s, s, s, s, s, s, z, z]

Final Array is [a, d, e, i, l, m, n, q, s, z]

 

 

1 comment:

  1. This 코인카지노 exciting collection highlights a free video games feature that's triggered by six or extra gold symbols. Capture a gold image in a pink frame and a 500x or 750x complete wager is awarded. At the tip of the bonus, any place with a green, silver or pink frame is changed with a firecracker image, offering the chance to receive as much as} a 7,500x wager multiplier with a triple firecracker image. Buy-a-pay slots are recognizable by the structure of their pay tables. Notice, for instance, that a Double Diamond slot has the identical symbols shown for one coin or two coin play. But a glance at|have a glance at} Bally's Flaming 7s machine, and you will notice the bars listed for one coin and the 7s listed for two coins.

    ReplyDelete