By Shahed Nooshmand

Perl Weekly Challenge: week 74

I was busy.

Task #1

You are given an array of integers of size $N.

Write a script to find the majority element. If none found then print -1.

Majority element in the list is the one that appears more than floor(size_of_list/2).

Here’s my solution:

(sub { .return if @_.grep($_) > @_/2 for @_; -1 })(@A)

Task #2

You are given a string $S.

Write a script to print the series of first non-repeating character (left -> right) for the given string. Print # if none found.

Which side’s right, again?

([\,] $S.comb).map({ .grep({ .grep($^c) == 1 })[*-1] // '#' }).join

I suggest you take a look at triangular reduction if you’re confused.