Java: Print a unique character in a string

前端 未结 17 1175
长情又很酷
长情又很酷 2021-01-03 00:03

I\'m writing a program that will print the unique character in a string (entered through a scanner). I\'ve created a method that tries to accomplish this but I keep getting

相关标签:
17条回答
  • 2021-01-03 00:48
    
    package extra;
    
    public class TempClass {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String abcString="hsfj'pwue2hsu38bf74sa';fwe'rwe34hrfafnosdfoasq7433qweid";
            char[] myCharArray=abcString.toCharArray();
            TempClass mClass=new TempClass();
    
            mClass.countUnique(myCharArray);
            mClass.countEach(myCharArray);
        }
        /**
         * This is the program to find unique characters in array.
         * @add This is nice.
         * */
        public void countUnique(char[] myCharArray) {
            int arrayLength=myCharArray.length;
            System.out.println("Array Length is: "+arrayLength);
            char[] uniqueValues=new char[myCharArray.length];
            int uniqueValueIndex=0;
            int count=0;
            for(int i=0;i<arrayLength;i++) {
                for(int j=0;j<arrayLength;j++) {
                    if (myCharArray[i]==myCharArray[j] && i!=j) {
                        count=count+1;
                    }
                }
                if (count==0) {
                    uniqueValues[uniqueValueIndex]=myCharArray[i];
                    uniqueValueIndex=uniqueValueIndex+1;
                    count=0;
                }
                count=0;
            }
            for(char a:uniqueValues) {
                System.out.println(a);
            }
        }
        /**
         * This is the program to find count each characters in array.
         * @add This is nice.
         * */
        public void countEach(char[] myCharArray) {
    
        }
    }
    
    
    0 讨论(0)
  • 2021-01-03 00:48

    Here str will be your string to find the unique characters.

    function getUniqueChars(str){
    let uniqueChars = '';
    for(let i = 0; i< str.length; i++){
      for(let j= 0; j< str.length; j++) {
        if(str.indexOf(str[i]) === str.lastIndexOf(str[j])) {
           uniqueChars += str[i];
         }
       }
     }
     return uniqueChars;    
    

    }

    0 讨论(0)
  • 2021-01-03 00:50

    How about applying the KISS principle:

    public static void uniqueCharacters(String test) {
        System.out.println(test.chars().distinct().mapToObj(c -> String.valueOf((char)c)).collect(Collectors.joining()));
    }
    
    0 讨论(0)
  • 2021-01-03 00:51

    I would store all the characters of the string in an array that you will loop through to check if the current characters appears there more than once. If it doesn't, then add it to temp.

    public static void uniqueCharacters(String test) {
        String temp = "";
        char[] array = test.toCharArray();
        int count; //keep track of how many times the character exists in the string
    
        outerloop: for (int i = 0; i < test.length(); i++) {
            count = 0; //reset the count for every new letter
            for(int j = 0; j < array.length; j++) {
                if(test.charAt(i) == array[j])
                    count++;
                if(count == 2){
                    count = 0;
                    continue outerloop; //move on to the next letter in the string; this will skip the next two lines below
                }
            }
            temp += test.charAt(i);
            System.out.println("Adding.");
        }    
        System.out.println(temp);
    }
    

    I have added comments for some more detail.

    0 讨论(0)
  • 2021-01-03 00:55
    public static String input = "10 5 5 10 6 6 2 3 1 3 4 5 3";
    
    public static void uniqueValue (String numbers) {
        String [] str = input.split(" ");
        Set <String> unique = new HashSet <String> (Arrays.asList(str));
        System.out.println(unique);
    
        for (String value:unique) {
            int count = 0;
            for ( int i= 0; i<str.length; i++) {
                if (value.equals(str[i])) {
                    count++;
                }
            }
            System.out.println(value+"\t"+count);
        }
    }
    public static void main(String [] args) {
        uniqueValue(input);
    }
    
    0 讨论(0)
提交回复
热议问题