Hello All,
I have stuck in a problem.
There are set of strings of different length. I have to remove repeated elements from all the strings except from the string in which it appeared first.
For example say the input strings are
"3 4 7 2 15 10"
"5 7 10 2"
"8 36 30 15 4"
"9 10 2 4 11 13 15"
"12 14 10 7 13 17 20 22 21 36"

then after the removal of repeated elements the output should be
"3 4 7 2 15 10"
"5"
"8 36 30"
"9 11 13"
"12 14 17 20 22 21"

Thanks in advance
Rajhans

I have the code but it will be very difficult to understand by other person as it is a intermediate part of a big code. That is why I have just put the problem part with a toy question and expected answer.

I have the code but it will be very difficult to understand by other person as it is a intermediate part of a big code. That is why I have just put the problem part with a toy question and expected answer.

That's not the only thing that's very difficult to understand...

Post the code you have, nobody is going to just give you the answers.

Edited 5 Years Ago by Syrne: n/a

Per our forum rules:
Keep It Organized - Do provide evidence of having done some work yourself if posting questions from assignments.

I have changed the code to make it simpler to understand. Hope You will be able to understand. other than that I am not an efficient programmer. I am from management side not from technical side thats why just usedthe basic function .

Code is as follows:

/

/kss[] contains set of sting ( input strings) 
//kpp[] contains the  set of desired strings ( i.e. output after removal of repeated elements) 
					
					
					//kppp[] is trim version of kpp[] and contain the after removal of  the initial space.
				// v is user defined parameter
// removeChar is a class used to remove element at a particular place.
// res [][] is a 2 D matrix that contain the membership values of different elements with //respect to different strings. The element with lower membership value is removed.
			 String kss[]=new String[v];
			String kpp[]=new String[v];
			String kt[]=new String[v];
			 BufferedReader bufferedReader1 = null;
			bufferedReader1 = new BufferedReader(new FileReader("c:\\hard.txt"));
			String line1 = null;
			int i1=0;
			 while ((line1 = bufferedReader1.readLine()) != null)
			 {
				 kss[i1]=line1; 
				 //System.out.println(kss[i1]);
				 i1++;
				
			 }
			
			 bufferedReader1.close();
			
			 int ippp=1;	
		//System.out.println("check0");
for(int ipp=0;ipp<i1;ipp++)
{
	//System.out.println(i1);
	int rp;
	rp=ipp;
	kpp[rp]=kss[ipp];
	int iz=0;
	kpp[ipp]=kss[ipp];
	int ixx=ippp;
	//if(kss[ipp]!=null)
	//{

		
		while(ippp<(i1+1))
	{
		if(kss[ippp]!=null)
		{
		int rint1=0;
		//System.out.println("check2");
		//System.out.println(kss[ipp]);
		//System.out.println(kss[ippp]);
		String[] temp1;
		String[] temp2;
		String delimiter=" ";
		
		temp1 = kpp[rp].split(delimiter);
		
		temp2 = kss[ippp].split(delimiter);
		//System.out.println("temp2 length =" +temp2.length);
		for (int ix=0;ix<temp1.length;ix++)
		{
			//System.out.println("check3");
			for (int iy=0;iy<temp2.length;iy++)
			{
				//System.out.println("check4");
				if(temp1[ix].equalsIgnoreCase(temp2[iy]))
				{
					//System.out.println("check5");
					String tempstr=temp1[ix];
				rint1=Integer.parseInt(tempstr);
				int sy1=Integer.parseInt(temp1[0]);
				int sy2=Integer.parseInt(temp2[0]);
                    
		if(res[sy1-1][(rint1-1)]<=res[sy2-1][(rint1-1)])
			 
					{
						
						removeChar rc=new removeChar();
						if(kpp[ipp]!=null)
						{
							iz=ix;
					kpp[rp]=rc.removeCharAt(kpp[rp],iz);
					kt[ipp]=kpp[rp].trim();
					kpp[rp]=kt[ipp];
					iz--;
					   }
					
						
					}
					
				}
				
			}
		iz++;
		}
	//iz=0;
	//ippp++;
		}
		iz=0;
	ippp++;
		}
		ippp=ixx;
		ippp++;
		
	}

for (int az=0; az<5;az++)
{
System.out.println(kpp[az]);
	
}

Ok, there is no way I'm going to try to trace that mess of meaningless variable names, but I will say that all you need is a List or Set to keep the numbers you have already encountered and the String.replace() method.

In fact, you only need the list if you want to retain a listing of the unique numbers. If you just want to cleanse repetitions, all you need to do is loop forward and remove them.

I agree with Ezzaral, please name your variables something significant...

Lol break down the strings using split, make string arrays and use a removeSimilar method once you got 2 arrays to compare... It's not that difficult of a process.

Edited 5 Years Ago by Nandomo: n/a

Have tried as per the advice of Ezzaral ( please see the code pasted below)but there is some problem in output.

kss[] is a set of input string

Input :
"3 4 7 2 15 10"
"5 7 10 2"
"8 36 30 15 4"
"9 10 2 4 11 13 15"
"12 14 10 7 13 17 20 22 21 36"

expected output
3 4 7 2 15 10
5
8 36 30
9 11 13
12 14 17 20 22 21
But actual output is not exactly same as expected
actual output happens to be
3 4 7 2 15 10
5
8 36 30
9 11 13
12 14 1 20 22 21

problem in last line .
Any suggestion please

for(int ipp=0;ipp<(length-1);ipp++) 
			 {
				 for (int is=ipp+1;is<length;is++)
				 
			 {
				 String[] temp1;
					String[] temp2;
					String delimiter=" ";
					
					temp1 = kss[ipp].split(delimiter);
					
					
					temp2 = kss[is].split(delimiter);
					for (int ix=0;ix<temp1.length;ix++)
					{
						for (int iy=0;iy<temp2.length;iy++)
						{
							if(temp1[ix].equalsIgnoreCase(temp2[iy]))
							{
								String tempstr=temp1[ix];
							kss[is]=kss[is].replaceAll(tempstr, "");
							kss[is]=kss[is].replaceAll(" \\s+", " ");// \s+ is a regular expression. \s matches a space, tab, new line, carriage return, form feed and vertical tab and + says "one or more of those". Thus it will collapse all "whitespace substrings" longer than one character, with a single space character.
							}                        //http://stackoverflow.com/questions/3958955/how-to-remove-duplicate-white-spaces-in-string-using-java
						}
					}
			 }
			 }

Edited 5 Years Ago by rajhans: n/a

This article has been dead for over six months. Start a new discussion instead.