Interviews12 answers
We have an array of integers like shown below int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 }; Write logic to find the repeated integers and their count.
12 Answers
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
var count = arr.GroupBy(item => item).Select(grp => new {Number = grp.Key,numberCount = grp.Count() });
varaprasad mullapudiPosted May 11, 2016, 10:32 PM EDT
List numbers = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
var uni = from n in numbers
group n by n into g
let count = g.Count()
orderby g.Key
select new { Value = g.Key, Count = count };
foreach (var n in uni)
Console.WriteLine("Value: " + n.Value + " Count: " + n.Count);
Shivshanker CheralPosted Nov 24, 2015, 1:18 AM EST
List numbers = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
var uni = from n in numbers
group n by n into g
let count = g.Count()
orderby g.Key
select new { Value = g.Key, Count = count };
foreach (var n in uni)
Console.WriteLine("Value: " + n.Value + " Count: " + n.Count);
Shivshanker CheralPosted Nov 24, 2015, 1:15 AM EST
// This is optimized code
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
for (int i = 0; i < arr.Length;i++)
{
int tpt = 0;
if(arr.Contains(arr[i]))
{
tpt = arr.Where(x => x == arr[i]).ToArray().Count();
}
Console.WriteLine(string.Format("The value is {0} and count is {1}", arr[i], tpt));
}
Console.ReadLine();
}
Tanuj KhuranaPosted Aug 13, 2015, 5:01 AM EDT
var repeatativeValues = arr.GroupBy(t => t).ToList().Where(t => t.Count() > 1).ToList().Select(t => new { Value = t.Key, Count = t.Count() }).ToList();
NacxPosted Aug 3, 2015, 6:04 AM EDT
int[] Duplicatearr=new int[]{ };
for(int i = 0; i < arr.Length; i ) {
if(arr.Containt(arr[i]))
{
Duplicatearr.Add(a[i]);
}
}
Gokul RathodPosted Nov 25, 2014, 3:21 AM EST
private static void FindRepeatedNumber()
{
Dictionary duplicateNumbers = new Dictionary();
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
for (int i = 0; i 1)
{
Console.WriteLine("Here are list of repeated numbers with their count!");
foreach (var item in duplicateNumbers)
{
Console.WriteLine("This number:{0} is occured {1} time(s).", item.Key, item.Value + 1);
}
}
else
Console.WriteLine("Reapeated number(s) not found in the list!");
}
}
kumar Kashyap PandeyPosted Dec 31, 2013, 6:54 PM EST
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j
Subhranshu NathPosted Nov 26, 2013, 2:41 AM EST
foreach (int i in arr)
{
int rpt = arr.Where(val => val == i).ToArray().Length;
if(rpt > 1)
Console.WriteLine("Number ={0}, Occurrances ={1}",i,rpt);
arr = arr.Where(val => val != i).ToArray();
}
Aman JainPosted Oct 23, 2013, 2:26 AM EDT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public static void counter(int[] arr)
{
int [] arr2=new int[20];
int l = 0;
for (int i = 0; i < arr.Length ; i++)
{
int count=1;
foreach (int x in arr2)
{
if (x == arr[i])
{
i++;
continue;
}
}
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] == arr[j])
{
count++;
arr2[l++] = arr[i];
}
}
Console.WriteLine(arr[i]+" is repeted " + count + " Times");
}
}
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
counter(arr);
Console.ReadKey();
}
}
}
Abdul RizwanPosted Sep 10, 2013, 6:33 AM EDT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public static void counter(int[] arr)
{
int [] arr2=new int[20];
int l = 0;
for (int i = 0; i < arr.Length ; i++)
{
int count=1;
foreach (int x in arr2)
{
if (x == arr[i])
{
i++;
continue;
}
}
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] == arr[j])
{
count++;
arr2[l++] = arr[i];
}
}
Console.WriteLine(arr[i]+" is repeted " + count + " Times");
}
}
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
counter(arr);
Console.ReadKey();
}
}
}
Abdul RizwanPosted Sep 10, 2013, 6:29 AM EDT
public void Repeated()
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
for (int i = 0; i {
for (int j=i+1; j {
if(arr[i]==arr[j])
{
Console.WriteLine(arr[i]);
}
}
}
}
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13, 14, 15, 12, 16, 17, 18, 15, 19, 20 };
for (int i = 0; i
for (int j=i+1; j
if(arr[i]==arr[j])
{
Console.WriteLine(arr[i]);
}
}
}
}
Pavan RamamurthyPosted Aug 20, 2013, 10:39 PM EDT




