0

Possible Duplicate:
Any chance to get unique records using Linq (C#)?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WaysOf100
{
class WaysOf100Test
{
    static void Main(string[] args)
    {
        WaysOf100 test= new WaysOf100();
        test.Go();
        test.EliminateDuplicates();
    }
}

class WaysOf100
{        
    List<string> results = new List<string>();             

    public void Go()
    {
        int num = 5, temp=0;//to store the intermediate difference
        for (int i = 1; i <num; i++)
        {
            temp = num - i;
            for (int j = 1; j <= temp; j++)
            {
                if (temp % j == 0)
                {   
                    //Console.Write(i + " ");
                    string text = "";                        
                    text = i.ToString();                        
                    for (int k = 1; k <= (temp / j); k++)
                    {
                        //Console.Write(j + " ");  
                        text += j.ToString();
                    }
                    char[] rev = text.ToCharArray();
                    Array.Reverse(rev);                       
                    if(!(results.Contains(rev.ToString())))
                        results.Add(text);                        

                }
            }                
        }
    }

    public void EliminateDuplicates()
    {            
        //To eliminate the duplicates   

        /*for (int i = 0; i < results.Count; i++)
        {

            for (int j = 0; j < results.Count; j++)
            {

                if (!(results[i].Equals(results[j])))
                {
                    char [] rev = results[j].ToCharArray();
                    Array.Reverse(rev);                        
                    if (results[i]==rev.ToString())
                        results.Remove(rev.ToString());
                }                                       
            }
        }*/

        foreach (var result in results)
        {
           Console.WriteLine(result);
        }
        Console.WriteLine("Total number of elements is :{0}",results.Count);
    }
}

}

The result so far is 11111 122 14 2111 23 311 32 41

This is what I want in short : the reverse of 41 is 14 and 14 already exists in the list so i don't want to add 41. Similarly, the reverse of 32 is 23 which also exists and hence 32 should not be added. But this piece of could which I've written to achieve the functionality is not giving the desired results

if(!(results.Contains(rev.ToString())))
      results.Add(text);
Community
  • 1
  • 1
kunaguvarun
  • 703
  • 1
  • 10
  • 29
  • 1
    This looks a lot like homework. – qJake May 25 '12 at 15:59
  • 2
    Title makes little sense. You don't state what you want. – paparazzo May 25 '12 at 16:04
  • Title is clear. He wants to remove the duplicate elements from his list of results. The list of results need to be unique, forwards and backwards. – StarPilot May 25 '12 at 16:08
  • 2
    Remove duplicates from "C# program" is not remove duplicate from "list". And there is no removal - rather creation of list of unique numbers with unique even if ordeer is reversed. – paparazzo May 25 '12 at 16:19
  • sorry to all for not describing what I need precisely – kunaguvarun May 25 '12 at 16:35
  • Would `122` and `212` be considered the same? Are you looking for unique combinations? So `1010` and `1100` would be the same, as well? – Jim Mischel May 25 '12 at 16:39
  • @varunit: if this is homework, please tag it as such and while you're at it, edit the title to indicate what you're really looking for. That said, what exactly is giving you trouble? Which part are you stuck on? – SirPentor May 25 '12 at 16:43
  • @SirPentor, the checking for element which already exists in reverse format of current number is what I need. This is not a home work. It is project euler's problem to represent 100 in different ways. I'm first trying to get the valid combinations for 5 – kunaguvarun May 25 '12 at 16:54

3 Answers3

1

The problem you are having is that rev.ToString()' returns "System.Char[]" and not the string value you wanted/expected. For your logic, try the following:

    public void EliminateDuplicates()
    {
        //Eliminate the duplicates    

        for (int i = 0; i < results.Count; i++)
        {
            for (int j = 0; j < results.Count; j++)
            {
                if (!(results[i].Equals(results[j])))
                {
                    char[] rev = results[j].ToCharArray();
                    char[] forward = results[i].ToCharArray();
                    Array.Reverse(rev);
                    bool bEqual = true;
                    for( int n = 0 ; n < results[j].Length && true == bEqual ; n++ )
                    {
                        if( rev[n] != forward[n] )
                        {
                            bEqual = false;
                        }
                    }
                    if( true == bEqual)
                        results.Remove(results[j] );
                }
            }
        }

        foreach (var result in results)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine("Total number of elements is : {0} ", results.Count);
    }
StarPilot
  • 2,246
  • 1
  • 16
  • 18
  • makes sense and got a feel that this will work for me sure. But when I hover my mouse over `rev.ToString()` the popup displays that return type is string. – kunaguvarun May 25 '12 at 17:00
  • please don't look at the commented code. I had tried it earlier and decided to do in another way. Please help me how to improve this piece of code `if(!(results.Contains(rev.ToString())))` `results.Add(text);` – kunaguvarun May 25 '12 at 17:04
  • The hover value you were seeing in the debugger was for rev, not rev.ToString(). You can test that for yourself by using `string tempString = rev.ToString()` and looking at the value actually assigned to `tempString`. – StarPilot May 25 '12 at 17:48
1

Solved by myself finally..

if (!(results.Contains(new string(rev))))
    results.Add(text);

changed the rev.ToString() as new string(rev) and works fine now. What I want is achieved. Thanks a lot for the help guys

kunaguvarun
  • 703
  • 1
  • 10
  • 29
0

Is reverse the only case you want to check for? One approach would be to canonicalize your results to e.g. sorted order before comparing. So transform both 132 and 213 to 123 before comparing.

bmm6o
  • 6,187
  • 3
  • 28
  • 55