2010-04-16 12 views
13

कैसे उपयोग करते हुए सी # एक इंट सरणी में सबसे आम मूल्य प्राप्त करने केइंटेल सरणी में सबसे आम मूल्य कैसे प्राप्त करें? (सी #)

जैसे: सरणी निम्न मान है: 1, 1, 1, 2

उत्तर होना चाहिए 1

+0

आपकी पूर्णांक मूल्यों के डोमेन पर एक प्रतिबंध है? अर्थात। 0 और 10 के बीच के सभी मूल्य हैं? –

+0

@ माइकल पेटीटो: हाँ। यदि सीमा बहुत बड़ी नहीं है, तो यह वास्तव में जल्दी किया जा सकता है। –

+0

सभी int सकारात्मक और मूल्य 5 – mouthpiec

उत्तर

26
var query = (from item in array 
     group item by item into g 
     orderby g.Count() descending 
     select new { Item = g.Key, Count = g.Count() }).First(); 

सिर्फ मूल्य और नहीं गिनती के लिए, आप दूसरे पर

var query = (from item in array 
       group item by item into g 
       orderby g.Count() descending 
       select g.Key).First(); 

लैम्ब्डा संस्करण कर सकते हैं:

var query = array.GroupBy(item => item).OrderByDescending(g => g.Count()).Select(g => g.Key).First(); 
+0

+1 मेरे से बेहतर, बेहतर और तेज है। –

+0

क्या यह ओ (nlogn) सॉर्टिंग नहीं कर रहा है? – liori

+1

@ लिओरी: हां। छंटनी उच्चतम गिनती खोजने का सबसे प्रभावी तरीका नहीं है। – Guffa

14

कुछ पुराने जमाने कुशल पाशन:

var cnt = new Dictionary<int, int>(); 
foreach (int value in theArray) { 
    if (cnt.ContainsKey(value)) { 
     cnt[value]++; 
    } else { 
     cnt.Add(value, 1); 
    } 
} 
int mostCommonValue = 0; 
int highestCount = 0; 
foreach (KeyValuePair<int, int> pair in cnt) { 
    if (pair.Value > highestCount) { 
     mostCommonValue = pair.Key; 
     highestCount = pair.Value; 
    } 
} 

अब mostCommonValue सबसे आम मूल्य होते हैं, और highestCount शामिल यह कितनी बार हुई।

+1

+1 कोहनी ग्रीस को बाहर निकालने और इसे करने के साथ कुछ भी गलत नहीं है। –

+0

'MaxBy()' का उपयोग कर उस दूसरे भाग को सरल बनाया जा सकता है। बहुत बुरा यह वास्तव में LINQ में नहीं है (लेकिन यह [MoreLinq] में है (http://code.google.com/p/morelinq/wiki/OperatorsOverview))। – svick

1

शायद O (n n लॉग इन करें), लेकिन तेजी से:

sort the array a[n] 

// assuming n > 0 
int iBest = -1; // index of first number in most popular subset 
int nBest = -1; // popularity of most popular number 
// for each subset of numbers 
for(int i = 0; i < n;){ 
    int ii = i; // ii = index of first number in subset 
    int nn = 0; // nn = count of numbers in subset 
    // for each number in subset, count it 
    for (; i < n && a[i]==a[ii]; i++, nn++){} 
    // if the subset has more numbers than the best so far 
    // remember it as the new best 
    if (nBest < nn){nBest = nn; iBest = ii;} 
} 

// print the most popular value and how popular it is 
print a[iBest], nBest 
+0

आपने पहले सरणी को सॉर्ट नहीं किया था :)। वैसे भी, अगर आप सॉर्ट करने जा रहे हैं तो आप यह आसान कर सकते हैं। लूप के लिए एक और कुछ चर पर्याप्त होना चाहिए। – IVlad

+0

@IVlad: कोड की पहली पंक्ति नहीं थी? वैसे भी, आप सही हैं। –

1
public static int get_occure(int[] a) 
    { 
     int[] arr = a; 
     int c = 1, maxcount = 1, maxvalue = 0; 
     int result = 0; 
     for (int i = 0; i < arr.Length; i++) 
     { 
      maxvalue = arr[i]; 
      for (int j = 0; j <arr.Length; j++) 
      { 

       if (maxvalue == arr[j] && j != i) 
       { 
        c++; 
        if (c > maxcount) 
        { 
         maxcount = c; 
         result = arr[i]; 

        } 
       } 
       else 
       { 
        c=1; 

       } 

      } 


     } 
     return result; 
    } 
1

मैं जानता हूँ कि इस पोस्ट पुरानी है, लेकिन किसी ने मुझे इस सवाल का उल्टा पूछा आज।

LINQ समूहीकरण

sourceArray.GroupBy(value => value).OrderByDescending(group => group.Count()).First().First(); 

अस्थायी संग्रह, Guffa के समान:

var counts = new Dictionary<int, int>(); 
foreach (var i in sourceArray) 
{ 
    if (!counts.ContainsKey(i)) { counts.Add(i, 0); } 
    counts[i]++; 
} 
return counts.OrderByDescending(kv => kv.Value).First().Key; 
संबंधित मुद्दे