12
Reply

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.

Pavan R Bhupalam

Pavan R Bhupalam

11y
7.5k
2
Reply

    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(); }

    // 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();}


     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 };var count = arr.GroupBy(item => item).Select(grp => new {Number = grp.Key,numberCount = grp.Count() });

    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 numbersgroup n by n into glet count = g.Count()orderby g.Keyselect new { Value = g.Key, Count = count };foreach (var n in uni)Console.WriteLine("Value: " + n.Value + " Count: " + n.Count);

    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 numbersgroup n by n into glet count = g.Count()orderby g.Keyselect new { Value = g.Key, Count = count };foreach (var n in uni)Console.WriteLine("Value: " + n.Value + " Count: " + n.Count);

    var repeatativeValues = arr.GroupBy(t => t).ToList().Where(t => t.Count() > 1).ToList().Select(t => new { Value = t.Key, Count = t.Count() }).ToList();

    int[] Duplicatearr=new int[]{ }; for(int i = 0; i < arr.Length; i ) { if(arr.Containt(arr[i])) {Duplicatearr.Add(a[i]); }}

    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);}}elseConsole.WriteLine("Reapeated number(s) not found in the list!");}}

    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

    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(); }} }

    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();} } }