Last active
August 23, 2020 14:46
-
-
Save jef-sure/0332b7709d2f38164437c74db94f0de1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use v5.14; | |
sub fnr_chars { | |
my $str = shift; | |
my @nr; | |
my %h; | |
join '', map { | |
push @nr, $_ if not exists $h{$_}; | |
$h{$_}++; | |
my $f; | |
while (@nr) { | |
$f = $nr[$#nr]; | |
last if $h{$f} == 1; | |
pop @nr; | |
$f = undef; | |
} | |
$f // '#'; | |
} split '', $str; | |
} | |
say fnr_chars('ababc'); | |
say fnr_chars('xyzzyx'); | |
my @A = (1, 2, 2, 3, 2, 4, 2); | |
my @B = (1, 3, 1, 2, 4, 5); | |
sub majority_el { | |
my $arr_ref = shift; | |
my %h; | |
$h{$_}++ for @$arr_ref; | |
my ($major) = grep {$h{$_} > @$arr_ref / 2} keys %h; | |
$major // -1; | |
} | |
say majority_el(\@A); | |
say majority_el(\@B); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment