c# - Finding the highest pair in int array -



c# - Finding the highest pair in int array -

i have dice game, need find highest pair of 5 dices , total of it. have made code far counts numbers pairs. want find highest pair.

code:

int pair[]; pair = new int[7] {0, 0, 0, 0, 0, 0, 0} //seven pairs because dont want utilize pair[0] int tt[]; tt = new int[5] { 1, 6, 3, 1, 3 }; //five dice int t = 1; for(int = 0; < 5; i++) { if ( tt[i] == t) { pair[t] = pair[t] + 1; t = t + 1; } }

if understand problem correctly, think overcomplicating it. if think it, want 2 highest numbers arry. sort it, , reverse it, , take 2 first numbers.

class programme { static void main(string[] args) { int[] dice = new int[5] { 1, 6, 3, 1, 3 }; array.sort(dice); array.reverse(dice); console.writeline("the largest pair ({0}, {1})", dice[0], dice[1]); } }

the other possibility largets pair (as mentioned in comment) want find highest number in list occurs twice. can sorting array, reversing it, read highest number. incrementally check if current number matches next number, , if case, found highest pair.

class programme { static void main(string[] args) { int[] dice = new int[5] { 1, 6, 3, 1, 3 }; array.sort(dice); array.reverse(dice); (int = 1; < dice.length; i++) { if (dice[i] == dice[i - 1]) { console.writeline("the largest pair ({0}, {1})", dice[i], dice[i-1]); break; } } } }

c# arrays pair

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -