#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Removing redundancy from String


    Hello all,

    I'm trying to eliminate the redundancy from this string using charAt. But can't quite nail it right. I'm trying to start with the first number in the string and cycle through all the others to make sure it's unique, and then if it is, write it out. Can you please take look and comment?

    Code:
    import java.io.*;
    class charAt{
        public static void main(String[] args) {
    String tf = "111111123422256";        
    
    for (int i=0;i<(tf.length()-1);i++){
    
    for (int z=0;z<(tf.length()-i);z++){
    
    char c = tf.charAt(i); char k = tf.charAt(i+z);
    
    		if(c==k){}
    
    		else{
    
    
    System.out.println(c);}}System.out.println(tf.charAt(tf.length()-1));
    
    }}}
    I realize this is off. The output should look like: 123456.

    Please help. Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Define another String variable "uniq". Iterate over String variable "tf". If the character in focus is already present in "uniq", then continue to next character; otherwise, append that character to "uniq".

    Code:
    public class TestClass {
      public static void main(String[] args) {
        String tf = "111111123422256";
        String uniq = "";
        int i = 0;
        for (i = 0; i < tf.length(); i++) {
          char c = tf.charAt(i);
          if (uniq.indexOf(c) > -1) {
            continue;
          }
          else {
            uniq += c;
          }
        }
        System.out.println(uniq);
      }
    }

    Comments on this post

    • kmath agrees : For whatever reason, I can't seem to add rep points for you, but good answer!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by noobie1000
    Define another String variable "uniq". Iterate over String variable "tf". If the character in focus is already present in "uniq", then continue to next character; otherwise, append that character to "uniq".

    Code:
    public class TestClass {
      public static void main(String[] args) {
        String tf = "111111123422256";
        String uniq = "";
        int i = 0;
        for (i = 0; i < tf.length(); i++) {
          char c = tf.charAt(i);
          if (uniq.indexOf(c) > -1) {
            continue;
          }
          else {
            uniq += c;
          }
        }
        System.out.println(uniq);
      }
    }

    That code works. But what if tf is something like

    Code:
    ABC_1426ABC_1427ABC_1428ABC_1430ABC_1432ABC_1433ABC_1434ABC_1435ABC_1436ABC_1437ABC_1438ABC_1439ABC_1440
    ?

    I tried using "substring" as well as changing indices but neither worked. Any ideas?

    Thanks,

    kmath
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    kmath agrees: For whatever reason, I can't seem to add rep points for you, but good answer!
    I think, that's because I've been active on this forum for less than a month now.

    Originally Posted by kmath
    ABC_1426ABC_1427ABC_1428ABC_1430ABC_1432ABC_1433ABC_1434ABC_1435ABC_1436ABC_1437ABC_1438ABC_1439ABC_ 1440
    In this case, what is the desired output?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    Originally Posted by kmath
    Hello all,

    I'm trying to eliminate the redundancy from this string using charAt. But can't quite nail it right. I'm trying to start with the first number in the string and cycle through all the others to make sure it's unique, and then if it is, write it out. Can you please take look and comment?

    Code:
    import java.io.*;
    class charAt{
        public static void main(String[] args) {
    String tf = "111111123422256";        
    
    for (int i=0;i<(tf.length()-1);i++){
    
    for (int z=0;z<(tf.length()-i);z++){
    
    char c = tf.charAt(i); char k = tf.charAt(i+z);
    
    		if(c==k){}
    
    		else{
    
    
    System.out.println(c);}}System.out.println(tf.charAt(tf.length()-1));
    
    }}}
    I realize this is off. The output should look like: 123456.

    Please help. Thanks.
    FYI, here's another way to do it.
    It benefits from a StringBuilder which doesn't create new strings everytime you append a character - unlike the existing way you're achieving it.

    Code:
    public static void createUniqueStringTest()
    	{
    		// Test Cases & Expected Output
    		String strOrig = "11112222334"; // Output = "1234"
    		String strOrig2 = "aaabbbcccccd"; // Output = "abcd"
    		String strOrig3 = "aaabbbacccd"; // Output = "abacd"
    		
    		// Create StringBuilder with the first character of the input.
    		// Because it is the first, it is guaranteed to be unique.
    		StringBuilder oOutput = new StringBuilder(String.valueOf(strOrig.charAt(0)));
    		int iPos = 0;
    		for (char cChar : strOrig.toCharArray())
    		{
    			// Skip the first character, we've already added it above
    			if (iPos == 0)
    			{
    				iPos++;
    				continue;
    			}
    			// If the current char does not equal the previous one in the original string, add it.
    			if (cChar != strOrig.charAt(iPos-1)) oOutput.append(cChar);
    			
    			iPos++;
    		}
    		
    		
    		System.out.println("Output: " + oOutput);
    	}
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Code:
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class ScanningText{
    
    	public static void main(String[] args) {
    String s;Scanner in=new Scanner(System.in);int x = 0;
    		//s = JOptionPane.showInputDialog("Enter a string");
            System.out.println("Enter String for compression"); 	
    s=JOptionPane.showInputDialog("Enter the String to be compressed ");
    //System.out.println(s); 
            int cn=1;int y=0;
           // s=s.toLowerCase();
            s+='x';
    		String ss="";
    		;StringBuilder build=new StringBuilder(ss);
    		char c = ' ';char t = ' ',jkl,jklm;
    		boolean b=true;
    	 //uber : while(b){
    		try{
    			cn=1;
    			outer:for(int j=0;j<(s.length()-1);j++){
    				
    			jkl=s.charAt(j);jklm=s.charAt(j+1);
    			if(jkl!=jklm)t=jkl; 
    				
    			inner:while(jkl==jklm){
    			cn++	;
                t=jkl	;
              continue outer;
    			}
    			//if(cn!=1)build.append((int)cn);	
    			//System.out.print(cn);
    			build.append(ss);
    			
    			cn=1;
    			build.append((char)t);
    			
    			continue outer;
    		//	jkl=s.charAt(cn);
    			//continue ;	
    		   
    		     
    		}
    	//}
    		
    			//if (x==s.length()-1)b=false;
    	}catch(Exception e){
    		
    	//	e.printStackTrace();
    	}
    		 System.out.print( build)	;	
    		 JOptionPane.showMessageDialog(null, "The Result of Compression is :  "+build);
    	 
    	 }
    	
    	}
    	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    //	}
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    // function takes a char array as input.
    // modifies it to remove duplicates and adds a 0 to mark the end
    // of the unique chars in the array.
    public static void removeDuplicates(char[] str) {
    if (str == null) return; // if the array does not exist..nothing to do return.
    int len = str.length; // get the array length.
    if (len < 2) return; // if its less than 2..can't have duplicates..return.
    int tail = 1; // number of unique char in the array.
    // start at 2nd char and go till the end of the array.
    for (int i = 1; i < len; ++i) {
    int j;
    // for every char in outer loop check if that char is already seen.
    // char in [0,tail) are all unique.
    for (j = 0; j < tail; ++j) {
    if (str[i] == str[j]) break; // break if we find duplicate.
    }
    // if j reachs tail..we did not break, which implies this char at pos i
    // is not a duplicate. So we need to add it our "unique char list"
    // we add it to the end, that is at pos tail.
    if (j == tail) {
    str[tail] = str[i]; // add
    ++tail; // increment tail...[0,tail) is still "unique char list"
    }
    }
    str[tail] = 0; // add a 0 at the end to mark the end of the unique char.
    }

    i found this it may help u
    Last edited by Winters; June 24th, 2013 at 09:58 AM. Reason: Removed fake sig

IMN logo majestic logo threadwatch logo seochat tools logo