Any efficient algorithm to achieve the following:-
Raw data:-
var arr = [
["one", "two", "three", "four", "five"] ,
["one", "two", "three", "six", "seven"] ,
["one", "two", "eight", "four", "ten"]
/* There can be more rows (arrays) */
];
Output:-
var arr2 = [
["one", "two"], /* this is because these are present in most of sub arrays (which in this case is 3 sub arrays) */
["three", "four"], /* after that three and four are present in most of subarrays (which in this case is 2 sub arrays) */
["five"], /* for those who have occurred one time order doesn't matter. Whichever comes first. */
["six"],
["seven"],
["eight"],
["ten"]
/* There can be even more rows... */
]
Conditions:-
- In a case if there is only one instance of an element in the whole multidimensional array then it should be appended to the output array (arr2) as a single array element. NOT with other elements (in a single array) who also exist alone. This is a special check only for single instance elements. If an element is present more than one time in the whole multidimensional array then it can be appended to the output array (arr2) with other elements, who occur the same number of times, in a single array. (Sorry for updating this condition so late. Thank you Joseph Mayer for pointing this out.)
Assumptions:-
- Element won't repeat in a subarray.