I did hard HackerRank exercise in Bash, and I made it even harder!

A week ago I started learning Bash, Linux Shell.

I have been doing HackerRank exercises since then.

A few minutes ago I did that exercise, which is labeled as Hard:
https://www.hackerrank.com/challenges/lonely-integer-2/problem?isFullScreen=true

There are N integers in an array A. All but one integer occur in pairs. Your task is to find the number that occurs only once.

Input Format

The first line of the input contains an integer N, indicating the number of integers. The next line contains N space-separated integers that form the array A.

I had been doing that exercise for more than hour before dinner. I was so numb that I was going already to resign, but when I came back with fresh mind, I did it... in 3 minutes.

Here is my solution:

```

read line
tr " " "\n" | sort -r | uniq -c | sort | head -n 1 | rev | cut -d " " -f 1 | rev
```

As You can see, I made that exercise even harder because I completely did not use the first input N, which is the length of the rest of the input. I did everything without any loop, just by easy commands: : "tr", "sort", "uniq", "head" , "rev" and "cut".

I am very proud of myself now.


Photo is free for commercial use from pixabay.com

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center