Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Get the unique characters in a string and respecting the order

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get the unique characters in a string and respecting the order

    Hello! I am new to this forum. I am beginner in Java programming.

    I would like to get only unique characters in a string that means I would not have a repeating letters in my array.
    For instance, if I have string "aaaabbbbccc", I only need to have 'a' 'b' and 'c' in my output

    Moreover, the order of the letters in the string is to be respected. For instance, If my string is "znnmzzmnn", The output is 'z' followed by 'n' and 'm'.

    This is just a part of the code I am doing in my project i.e Hufman coding. My code should read the string from a text file actually which I will do in the future after I solve this. My prof told us not to use subclasses and collections. I also wanted to ask if there is other way to do it aside from array especially different text file can have different amounts of letters

    	String line="abcdabcdefghij";
     
    	char []letter=line.toCharArray();
    	char []uniqueLetter=new char[10];
     
     
    	System.out.println("The unique letters in sequence are ");
       	for(int i=0;i<uniqueLetter.length;i++)
    	{
     
    	 for(int j=i;j<letter.length;j++)
    	 {
    		 if(uniqueLetter[i]!=letter[j])
    		  uniqueLetter[i]=letter[j];
    	 } 
     
    	 System.out.println( "index "+i+": "+ uniqueLetter[i]);
    	}

    I am getting output:

    #The unique letters in sequence are
    index 0: j
    index 1: j
    index 2: j
    index 3: j
    index 4: j
    index 5: j
    index 6: j
    index 7: j
    index 8: j
    index 9: j


    Output I should be getting is:

    #The unique letters in sequence are
    index 0: a
    index 1: b
    index 2: i
    index 3: c
    index 4: d
    index 5: e
    index 6: f
    index 7: g
    index 8: h
    index 9: j


    Advanced Thanks!!!
    Last edited by slashGamer; August 4th, 2017 at 01:32 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Get the unique characters in a string and respecting the order

    Can you describe the steps you would take if you were to do the search manually?
    Work out the logic before trying to write the code.

    Why is the length of uniqueLetter array set to 10? Instead of 5 or 20???
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Characters from String in alphabetic order using quicksort
    By yellowglow in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 2nd, 2014, 09:35 AM
  2. Ignore certain characters in a string.
    By JaAnTr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2014, 03:18 PM
  3. Eliminating Unicode Characters and Escape Characters from String
    By bilalonjavaforum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 19th, 2013, 05:26 AM
  4. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM