Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Get missed element in integer collection
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Data; namespace Rextester { public class Program { public static void Main(string[] args) { var n = 10; var coll = new int[n]; for(var i = 0; i < n; i++){coll[i]=i+1;} coll[5] = 3; Console.WriteLine(GetMissedUnsorted(coll)); //Array.Sort(coll); Console.WriteLine(GetMissedUnsorted2(coll)); } public static int GetMissedUnsorted(int[] coll){ var n = coll.Length; var missed = -1; var sum = (n * (n+1)) / 2; var sumOrigin = 0; //Your code goes here foreach(var i in coll.Distinct()){ sumOrigin += i; } return sum - sumOrigin; } public static int GetMissedSortedDirrect(int[] coll){ //Your code goes here var result = 0; foreach(var i in coll){ if(result == i) return i+1; result = i; } return result; } public static int GetMissedUnsorted2(int[] coll){ //Your code goes here for(var i = 0; i < coll.Length; i++){ if(coll[i]!=i + 1) return i + 1; } throw new Exception("The missed element not found!"); } } }
run
|
edit
|
history
|
help
0
Classes-and-Structures.cs
sdfgtrewqdsc sdzdxsd
fun with yield return
5
Celsius to Kelvin / Kelvin to Celsius
asdcsadxasxd
Problem_binary
Encryption & Decryption AES C#
seoituerwoi
Date Comparison