Introduction
In this article I explain about an array that contains various duplicate elements and I want to separate each duplicate and non-duplicate element of the array. So I have stored duplicate and non-duplicate elements in separate collections.
Use the following procedure to create it.
Step 1
First of all you must create a new Windows Store Application.
- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose "Template" -> "Visual C#" -> "Window Store app"
- Choose "Blank App (XAML)" then rename the application
Step 2
Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):
<Page
x:Class="remove_duplicate_element_of_an_array.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:remove_duplicate_element_of_an_array"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Red">
<Grid.RowDefinitions>
<RowDefinition Height="110*"/>
<RowDefinition Height="59*"/>
<RowDefinition Height="40*"/>
<RowDefinition Height="45*"/>
<RowDefinition Height="514*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="167*"/>
<ColumnDefinition Width="248*"/>
<ColumnDefinition Width="268*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Array content is :" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Column="1" Grid.Row="1" TextAlignment="Left" Margin="0,0,375,12" Grid.ColumnSpan="2"/>
<TextBlock x:Name="text1" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Row="1" Margin="10,0,-57,0" Grid.Column="2" Width="583" VerticalAlignment="Top" HorizontalAlignment="Left" Height="54" />
<TextBlock x:Name="text2" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="583" Margin="0,0,-47,0"></TextBlock>
<TextBlock Text="After remove duplicate element array is :" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" Foreground="White" HorizontalAlignment="Left" Width="496" />
<TextBlock Text="Total duplicat element is : " FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="3"></TextBlock>
<TextBlock x:Name="text3" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="583" Margin="0,0,-47,0"></TextBlock>
</Grid>
</Page>
Step 3
Now write the following C# code for the button within "Mainpage.Xaml.cs".
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections;
using System.Collections.Generic;
namespace remove_duplicate_element_of_an_array
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public void checkduplicate(int[] arr1)
{
foreach (int a in arr1)
{
text1.Text += a + " ";
}
foreach (int a in arr1)
{
if (!nondupelement.Contains(a))
{
nondupelement.Add(a);
text2.Text += a + " ";
}
else if(!dupelement .Contains (a))
{
dupelement.Add(a);
text3.Text += a + " ";
count++;
}
}
}
List<int> dupelement = new List<int>();
List<int> nondupelement = new List<int>();
static int count = 0;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
int[] arr = { 14, 14, 65, 7, 8, 7, 47, 14, 7, 8 };
checkduplicate(arr);
}
}
}
Step 4
Now Run your app.