'How showing karakter duplicate wtihout remove in C#
This My Code C#:
static void Main(string[] args)
{
string angka, MissChar;
lagi:
Console.Write("Masukkan Angka : ");
angka = Console.ReadLine().ToLower().Replace(',', '.').Replace(' ', '.').Replace('.', '.');
angka = angka.ToLower().Replace(',', '.').Replace('-', '.').Replace('@', '.').Replace('/', '.').Replace('_', '.').Replace(" ", ".").Replace('\n', '.').Replace("X.", "x").Replace(".X", "x").Replace("x.", "x").Replace(".x", "x").Replace(".x.", "x").Replace(".X.", "x").Replace("X", "x").Replace("*", "x").Replace("*.", "x").Replace(".*.", "x").Replace("×", "x").Replace(".×", "x").Replace("×.", "x").Replace(".×.", "x").Replace("..X", "x");
angka = Regex.Replace(angka, @"\.+", ".");
//
string temp = "";
bool isJumpaX = false;
List<string> iniFinal = new List<string>();
for (int j = 0; j < angka.Length; j++)
{
char kar = angka[j];
if (kar == '.' && isJumpaX)
{
iniFinal.Add(temp);
temp = "";
isJumpaX = false;
continue;
}
if (kar == 'x' || kar == 'X' || kar == '×')
{
isJumpaX = true;
}
temp += kar;
}
string penampung = "";
Dictionary<string, int> freq = new Dictionary<string, int>();
foreach (var word in iniFinal)
{
string[] resSplitByX = word.Split('x', ' ');
double totalKarakter = resSplitByX[0].Split('.').Length;
string[] resLeft = resSplitByX[0].Split('.');
//
Dictionary<string, bool> unik = new Dictionary<string, bool>();
foreach (var cAngka in resLeft)
{
unik[cAngka] = false;
}
foreach (var cAngka in resLeft)
{
if (!unik[cAngka])
{
if (!freq.ContainsKey(cAngka))
{
freq[cAngka] = 1;
}
else
{
freq[cAngka] += 1;
}
unik[cAngka] = true;
}
}
double right = Convert.ToDouble(resSplitByX[1]);
double hasilKalkulasi = totalKarakter * right;
}
List<string> lstGanda = new List<string>();
HashSet<string> listNomorGanda = new HashSet<string>();
foreach (var word in iniFinal)
{
string[] resSplitByX = word.Split('x');
string[] resLeft = resSplitByX[0].Split('.');
foreach (var cAngka in resLeft)
{
if (freq[cAngka] >= 2)
{
listNomorGanda.Add(word);
}
}
}
foreach (var ganda in listNomorGanda)
{
Console.WriteLine("Ganda : {0}", ganda);
}
}`
If im input : 11.x8.22.33.44.55.66.x3.22.44.66.x4.22.x2.55.x3.66.44x2.55x3.33x3.33x3.22x3.33.55.x3.55x2.66x50...
And i want output showing duplicat, but i showing without remove. Result output : 22x3 22x4 22x2 22x3 33x3 33x3 33x3 33x3 44x3 44x4 44x2 55x3 55x2 55x3 55x3 55x3 66x50 66x3 66x4 66x2
Result output can change if input change, Please HELP ME!! PLEASE ANYONE
Solution 1:[1]
If I'm guessing correctly, you want to only show the duplicates in your input. To do so, I made an update to your code, and explain how it works.
First : We have to process the input a bit to clean it and make it usable, we will take your input as an example.
string input = "11.x8.22.33.44.55.66.x3.22.44.66.x4.22.x2.55.x3.66.44x2.55x3.33x3.33x3.22x3.33.55.x3.55x2.66x50.";
string[] cleanedData = Regex.Split(input.Replace('.', ' '), "(x[0-9]+)").Select(elem => elem.Trim()).Where(elem => elem != "").ToArray();
So, first, I replaced all the points in the string by blank space. Then, I'm spliting the data when we have a multiplicator appearing. The regex (x[0-9]+) means : parenthesis keeps the delimitor when we split, "x" is the first character of the delimitor, [0-9] means that after this character I'm looking for a number between 0 and 9 and + means I'm looking for at least one number.
I trimmed them after using the select. The main point is to avoid to have useless / problematic characters for the futur split phase. Then, using the Where function, I'm filtering the data to pop out the potential empty string.
At this point, our list cleanedData looks like this :
[11, x8, 22 33 44 55 66, x3, 22 44 66, x4, 22, x2, 55, x3, 66 44, x2, 55, x3, 33, x3, 33, x3, 22, x3, 33 55, x3, 55, x2, 66, x50]
As we can see, we car go through this list two elements by two elements. The first will be the numbers we want to multiply, the second will be the multiplicator.
List<string> result = new();
for(int i = 0; i < cleanedData.Length; i+=2)
{
string multiplicator = cleanedData[i + 1];
List<string> numbersToMultiply = cleanedData[i].Split(" ").ToList();
List<string> multiplications = leftParameters.Select(number => number + multiplicator).ToList();
result = result.Concat(multiplications).ToList();
}
So, in this second part, we have our list of results, each result will have the shape "axb". First, we are iterating two by two. We are transforming the string of numbers to a List of numbers (still as a string). Then, in the variable multiplications we are selecting all of them, and adding to their string, the multiplicator. In the end, we are adding our new multiplications to the list of results.
After the second step, our data looks like that :
[11x8, 22x3, 33x3, 44x3, 55x3, 66x3, 22x4, 44x4, 66x4, 22x2, 55x3, 66x2, 44x2, 55x3, 33x3, 33x3, 22x3, 33x3, 55x3, 55x2, 66x50]
In the last step, we will keep only the duplicates in our list.
result = result.GroupBy(s => s).SelectMany(grp => grp.Skip(1)).ToList();
Console.WriteLine(string.Join(" ", result));
We will continue to use the LINQ functions. With GroupBy, we will group together the same string. We will use the SelectMany to get our groups, and with the Skip(1) function, we will get, foreach group the next elements after the first. So if there is only one element, the grp.Skip(1) will return an empty list. So this way, we are keeping only the duplicates elements.
After the execution, our printed result looks like that :
22x3 33x3 33x3 33x3 55x3 55x3 55x3
Here is the full code :
string input = "11.x8.22.33.44.55.66.x3.22.44.66.x4.22.x2.55.x3.66.44x2.55x3.33x3.33x3.22x3.33.55.x3.55x2.66x50.";
string[] cleanedData = Regex.Split(input.Replace('.', ' '), "(x[0-9]+)").Select(elem => elem.Trim()).Where(elem => elem != "").ToArray();
List<string> result = new();
for(int i = 0; i < cleanedData.Length; i+=2)
{
string multiplicator = cleanedData[i + 1];
List<string> numbersToMultiply = cleanedData[i].Split(" ").ToList();
List<string> multiplications = numbersToMultiply.Select(number => number + multiplicator).ToList();
result = result.Concat(multiplications).ToList();
}
result = result.GroupBy(s => s).SelectMany(grp => grp.Skip(1)).ToList();
Console.WriteLine(string.Join(" ", result));
I hope I've understood well your problem. Have a good day !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Clement |
