gw punya kasus kya gini gay : temukan bilangan yang hanya muncul 1 kali pada larik {1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}
klo lu pke bruteforce, mungkin kurang lebih algo lu hampir sama kya punya ane:
using System;
using System.Collections.Generic;
using System.Linq;
class test{
static void Main(){
int[] larik=new int[]{1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8};
List<int> stack=new List<int>();
foreach(int i in larik)
if(!stack.Contains(i))
stack.Add(i);
int[] jumlah=new int[stack.Count];
for(int i=0; i<stack.Count; i++){
for(int j=0; j<larik.Length; j++){
if(stack[i]==larik[j])jumlah[i]++;
}
}
for(int i=0; i<stack.Count; i++)
if(jumlah[i]==1)Console.Write(stack[i]+" ");
}
}
tapi klo lu pke Linq, cuman sebaris doank gay
using System;
using System.Collections.Generic;
using System.Linq;
class test{
static void Main(){
int[] larik=new int[]{1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8};
var l=larik.Distinct().Where(r=>larik.Where(i=>i==r).Count()==1);
Console.Write(string.Join(",",l.ToArray()));
}
}
yah menurut gw Linq itu hampir sama lah kya di sql gitu, select2an,where2an,dll