cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove duplicate rows from node?

Former Member
0 Kudos

Hello all,

how can I remove rows that contain the same entries from single node?

for example:

NamesNode

|___id

|___name

|___year

id column name colum year column

12345 aaa 1990

34567 bbb 1995

12345 aaa 1990

22222 ccc 2001

12345 aaa 1990

after removing I will get:

id column name colum year column

12345 aaa 1990

34567 bbb 1995

22222 ccc 2001

Hope your help & Thanks in advance.

Regards,

Sam

Accepted Solutions (1)

Accepted Solutions (1)

gill367
Active Contributor
0 Kudos

Hi Sam,

You could do it in the following way:-

1. Create a node similar to this node for which you want unique records.

2. then write the following code in any eventhandler this will fill the new node with the unique records.

int size = wdContext.nodeA().size();
    boolean flag = false;
    String val1 = "", val2 = "",val3 = "", val4 = "";
	String nval1 = "", nval2 = "",nval3 = "", nval4 = "";
    for (int i = 0; i < size; i++)
    {  
    	flag = false;
    	val1 = wdContext.nodeA().getAElementAt(i).getA1();
    	val2 = wdContext.nodeA().getAElementAt(i).getA2();
		val3 = wdContext.nodeA().getAElementAt(i).getA3();
		val4 = wdContext.nodeA().getAElementAt(i).getA4();	
		
	for(int j = i+1; j< size; j++)
	{
		nval1 = wdContext.nodeA().getAElementAt(j).getA1();
		nval2 = wdContext.nodeA().getAElementAt(j).getA2();
		nval3 = wdContext.nodeA().getAElementAt(j).getA3();
		nval4 = wdContext.nodeA().getAElementAt(j).getA4();
		
		if((val1.equals(nval1))
		&&(val2.equals(nval2))
		&&(val3.equals(nval3))
		&&(val4.equals(nval4))
		)
		{
			flag = true;
		}
		
		
		
	}

	if(!flag )
	{
		IPrivateTest3View.IBElement b = wdContext.createBElement();
					b.setB1(val1);
					b.setB2(val2);
					b.setB3(val3);
					b.setB4(val4);
					wdContext.nodeB().addElement(b);
		
	}
    	
    }

hope this will solve your problem.

Sarbjeet

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi sarbjeet

I sorry, I didn't know simply I'm a new member..

so I gave points

Thanks,

Sam

Former Member
0 Kudos

Omri, Abhinav, Sarbjeet, Siarhei Hello

Thank you all for your help

Just today I got it and I've implemented the solution of Sarbjeet - cuz it's not need sorting - & the problem solved.

Really, I did't check the other solutions

Regards,

Sam

gill367
Active Contributor
0 Kudos

HI Siarhei ,

Thanks for the explanation. But could you tell me the way to implement the standard method.

and @ sam2010

People usually give points on getting the solution.

sarbjeet

Former Member
0 Kudos

Hi,

This would be a more generic function. Not sure about performance, it shouldn't be that bad. (But I'm sure it can be tuned)


  private void removeDuplicateElements(IWDNode node) {
	  List attributeNameList = new ArrayList();
	  Iterator nodeAttributes = node.getNodeInfo().iterateAttributes();
	  while (nodeAttributes.hasNext()) {
		  AttributeInfo attributeInfo = (AttributeInfo) nodeAttributes.next();
		  String attributeName = attributeInfo.getName();
		  attributeNameList.add(attributeName);
	  }
	  List sbAttributeList = new ArrayList();
	  for (int i = 0; i < node.size(); i++) {
		  IWDNodeElement nodeElement = node.getElementAt(i);
		  StringBuffer sbAttributeBuffer = new StringBuffer();
		  for (int j = 0; j < attributeNameList.size(); j++) {
			  String attributeName = (String) attributeNameList.get(j);
			  Object attributeValue = nodeElement.getAttributeValue(attributeName);
			  sbAttributeBuffer.append(attributeValue);
		  }
		  sbAttributeList.add(sbAttributeBuffer);
	  }
	  for (int i = 0; i < sbAttributeList.size(); i++) {
		  StringBuffer sbAttributeBuffer = (StringBuffer) sbAttributeList.get(i);
		  for (int j = i + 1; j < sbAttributeList.size(); j++) {
			  StringBuffer sbAttributeBufferCompare = (StringBuffer) sbAttributeList.get(j);
			  if (sbAttributeBuffer.toString().equals(sbAttributeBufferCompare.toString())) {
				  sbAttributeList.remove(j);
				  node.removeElement(node.getElementAt(j));
				  j--;	// RollBack Index..
			  }
		  }
	  }
  }

Hope it helps,

Daniel

siarhei_pisarenka3
Active Contributor
0 Kudos

@Omri, Obinav: +1 to your suggestions.

@Sarbjeet: I think that your custom sorting algorithm is not so effective as the standard one.

BR, Siarhei

gill367
Active Contributor
0 Kudos

HI Siarhei Pisarenka

Could you tell me why it is not an effective method.

Thanks,

Sarbjeet

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Sarbjeet

>Could you tell me why it is not an effective method.

The reason is in the algorithm's complexity. The standard sorting function has NLog(N) complexity. Your solution based on two iterations has NN.

NLog(N) << NN

That's why on a big number of elements the standard sorting function will be much faster.

BR, Siarhei

former_member182374
Active Contributor
0 Kudos

Hi,

The best way is to select only relevant records from the backend (R3/DB).

If you can't do that I guess you can sort the node and then compare node values 0 & 1, 1 & 2, 2 & 3 etc

Regards,

Omri

Abhinav_Sharma
Contributor
0 Kudos

As mentioned by Omri, the best way is to remove duplicates in backend only.

The other way is to compare the elements and find duplicates. You can add the selected elements into a ArrayList.

First sort the elements using sort function. Refer this thread

ArrayList finalList = new ArrayList();

for(int i = 0; i< nodeSize-1 ; i++){

element1 = wdContext.node<NodeName>.get<NodeName>ElementAt(i);

nextelement = wdContext.node<NodeName>.get<NodeName>ElementAt(i+1);

if(element1.getId().equals(nextelement.getId())

&& element1.getName().equals(nextelement.getName())

&& element1.getyear().equals(nextelement.getyear())){

continue;

}else{

finalList.add(element1);

}

}

finalList will contain distinct elements. You can disply this list in the table.

Let me know if it works.

Abhinav