Java Method for removing duplicates from char array

后端 未结 5 1162
礼貌的吻别
礼貌的吻别 2021-01-24 01:40

I have a char array filled by the user (arrayInput[]) with some characters, like {b, d, a, b, f, a, g, a, a, f}, and I need to create a method which returns a new c

相关标签:
5条回答
  • 2021-01-24 02:19

    Here's the method to achieve what you need:

    public static void main(String[] args) {
        char[] arr= {'A','B','C','A','B'};
    
        HashSet<Character> hset=new HashSet<Character>();
    
        for(int i=0;i<arr.length;i++) {
            hset.add(arr[i]);
            }
    
        Object[] ObjChar=hset.toArray();
    
        char[] resultArr = new char[ObjChar.length];
    
        for(int j=0;j<ObjChar.length;j++) {
    
        resultArr[j]=(char) ObjChar[j];
        }
    
        for(char eachChar: resultArr) {
    
            System.out.println(eachChar);
        }
    }
    
    0 讨论(0)
  • 2021-01-24 02:29

    This might help. Make a separate array and store only non-duplicate characters.

    char[] removeDuplicates (char[] arrayInput) {
        boolean exists[]=new boolean[26];
        char arrayOutput[] = new char[26];
        int ctr=0;
        for(int i=0; i<26; i++) {
            exists[i] = false;
        }
        for(int i=0; i<arrayInput.length; i++) {
            if(!exists[arrayInput[i]-97]) {
                exists[arrayInput[i]-97]=true;
                arrayOutput[ctr++]=arrayInput[i];
            }
        }
    
       return Arrays.copyOfRange(arrayOutput, 0, ctr);
    
    }
    
    0 讨论(0)
  • 2021-01-24 02:31
     boolean arr[26]; //considering only small letters arrive. otherwise take a larger array.
    for( i=0;i<str.length;i++ )
      arr[str[i]-'a']=true;
    

    The ones at last after the loop are true are the actual character. (all duplicates eleminated).

    To take into consideration the positions,

    int arr[26];
      //initialize all the array elemnts to 0
      for( i=0;i<str.length();i++ )
          if(i>=arr[str[i]-'a'])
                arr[str[i]-'a']=i+1;
    

    //Those greater than 0 are non-duplicated characters. Their poistion of first occurence= (arr[i]-1)

    EDIT: I have last used java almost a year ago. The algorithm is shown properly. Sorry for my awkward java code.

    0 讨论(0)
  • 2021-01-24 02:33

    This could work:

    public static void main(String[] args) {
        Main main = new Main();
        char[] array = {'e','a','b','a','c','d','b','d','c','e'};
        main.getCharArray(array);
    }
    
    private char[] getCharArray(char[] array) {
        String _array = "";
        for(int i = 0; i < array.length; i++) {
            if(_array.indexOf(array[i]) == -1) // check if a char already exist, if not exist then return -1
                _array = _array+array[i];      // add new char
        }
        return _array.toCharArray();
    }
    

    Output:

    eabcd

    0 讨论(0)
  • 2021-01-24 02:37

    If you consider using of collection framework then it would be much easier. Your array of char with duplicate is arrayInput. Now put each char from it to a HashSet like this -

    HashSet<Character> uniqueCharSet = new HashSet<Character>();
    for(char each : arrayInput){
    
       uniqueCharSet.add(each);
    }   
    

    Now the HashSet uniqueCharSet will contains only the unique characters from the char array arrayInput. Note here all element in uniqueCharSet are wrapper type - Character.

    You can convert the HashSet uniqueCharSet to array of Character like this -

    Object[] uniqueCharArray = uniqueCharSet.toArray();
    

    And then you can use them like this -

    for(Object each : uniqueCharArray){
       Character c = (Character) each;
       System.out.println(c);
    }
    
    0 讨论(0)
提交回复
热议问题