Remove duplicates from string array in Windows (C++/MFC/ATL)



Following code removes duplicates from string array in Windows (C++/MFC/ATL).

 
void RemoveDuplicatesFromArray(CAtlArray<CString> &arrValue)
{
	CAtlArray<CString> arrNew;
	INT_PTR i, j, iSize, iNewSize;
	CString strValue;
 
	BOOL bFound = FALSE;
 
	iSize = arrValue.GetCount();
	for(i=0;i<iSize;i++)
	{	
		strValue = arrValue[i];
		strValue.Trim();
		bFound = FALSE;
		iNewSize = arrNew.GetCount(); 
		for(j=0;j<iNewSize;j++)
		{
 
			if (strValue.CompareNoCase(arrNew[j]) == 0)
			{
				bFound = TRUE;
				break;
			}
		}
 
		if (!bFound)
		{
			arrNew.Add(strValue);
		}
	}
 
	//finally copy from new array to the existing array
	arrValue.RemoveAll();
	arrValue.Copy(arrNew);
 
}


Leave a Reply

Your email address will not be published. Required fields are marked *