Task: Cheryl's birthday





Java: import java.time.Month;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {
    private static class Birthday {
        private Month month;
        private int day;

        public Birthday(Month month, int day) {
            this.month = month;
            this.day = day;
        }

        public Month getMonth() {
            return month;
        }

        public int getDay() {
            return day;
        }

        @Override
        public String toString() {
            return month + " " + day;
        }
    }

    public static void main(String[] args) {
        List<Birthday> choices = List.of(
            new Birthday(Month.MAY, 15),
            new Birthday(Month.MAY, 16),
            new Birthday(Month.MAY, 19),
            new Birthday(Month.JUNE, 17),
            new Birthday(Month.JUNE, 18),
            new Birthday(Month.JULY, 14),
            new Birthday(Month.JULY, 16),
            new Birthday(Month.AUGUST, 14),
            new Birthday(Month.AUGUST, 15),
            new Birthday(Month.AUGUST, 17)
        );
        System.out.printf("There are %d candidates remaining.\n", choices.size());

        
        Set<Month> uniqueMonths = choices.stream()
            .collect(Collectors.groupingBy(Birthday::getDay))
            .values()
            .stream()
            .filter(g -> g.size() == 1)
            .flatMap(Collection::stream)
            .map(Birthday::getMonth)
            .collect(Collectors.toSet());
        List<Birthday> f1List = choices.stream()
            .filter(birthday -> !uniqueMonths.contains(birthday.month))
            .collect(Collectors.toList());
        System.out.printf("There are %d candidates remaining.\n", f1List.size());

        
        List<Birthday> f2List = f1List.stream()
            .collect(Collectors.groupingBy(Birthday::getDay))
            .values()
            .stream()
            .filter(g -> g.size() == 1)
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
        System.out.printf("There are %d candidates remaining.\n", f2List.size());

        
        List<Birthday> f3List = f2List.stream()
            .collect(Collectors.groupingBy(Birthday::getMonth))
            .values()
            .stream()
            .filter(g -> g.size() == 1)
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
        System.out.printf("There are %d candidates remaining.\n", f3List.size());

        if (f3List.size() == 1) {
            System.out.printf("Cheryl's birthday is %s\n", f3List.get(0));
        } else {
            System.out.println("No unique choice found");
        }
    }
}




VB: Private Sub exclude_unique_days(w As Collection)
    Dim number_of_dates(31) As Integer
    Dim months_to_exclude As New Collection
    For Each v In w
        number_of_dates(v(1)) = number_of_dates(v(1)) + 1
    Next v
    For i = w.Count To 1 Step -1
        If number_of_dates(w(i)(1)) = 1 Then
            months_to_exclude.Add w(i)(0)
            w.Remove i
        End If
    Next i
    For Each m In months_to_exclude
        exclude_month w, m
    Next m
End Sub
Private Sub exclude_month(x As Collection, v As Variant)
    For i = x.Count To 1 Step -1
        If x(i)(0) = v Then x.Remove i
    Next i
End Sub
Private Sub exclude_non_unique_days(w As Collection)
    Dim number_of_dates(31) As Integer
    For Each v In w
        number_of_dates(v(1)) = number_of_dates(v(1)) + 1
    Next v
    For i = w.Count To 1 Step -1
        If number_of_dates(w(i)(1)) > 1 Then
            w.Remove i
        End If
    Next i
End Sub
Private Sub exclude_non_unique_months(w As Collection)
    Dim months As New Collection
    For Each v In w
        On Error GoTo 1
        months.Add v(0), v(0)
    Next v
1:
    For i = w.Count To 1 Step -1
        If w(i)(0) = v(0) Then
            w.Remove i
        End If
    Next i
End Sub
Public Sub cherylsbirthday()
    Dim v As New Collection
    s = "May 15, May 16, May 19, June 17, June 18, July 14, July 16, August 14, August 15, August 17"
    t = Split(s, ",")
    For Each u In t
        v.Add Split(Trim(u), " ")
    Next u
    
    exclude_unique_days v
    
    exclude_non_unique_days v
    
    exclude_non_unique_months v
    Debug.Print v(1)(0); " "; v(1)(1)
End Sub