Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from MrPhantomT/feature/add_first_repeating_e…
…lement_problem add first repeating element problem
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
level: easy | ||
tags: [] | ||
description: | | ||
Given an array of integers, find the first repeating element in it. | ||
We need to find the element that occurs more than once and whose index of first occurrence is smallest. | ||
multicode_checks: | ||
langs: [ruby, python, javascript, php] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(ns battle-solutions.first-repeating-element-test | ||
(:require [clojure.test :refer :all] | ||
[battle-asserts.test-helper :refer :all])) | ||
|
||
(defn first-repeating-element [array] | ||
(->> | ||
array | ||
(reduce #(assoc %1 %2 (inc (get %1 %2 0))) {}) | ||
(filter #(> (second %) 1)) | ||
ffirst)) | ||
|
||
(deftest test-asserts | ||
(assert-equal 5, (first-repeating-element [10 5 3 4 3 5 6])) | ||
(assert-equal 6, (first-repeating-element [6 10 5 4 9 120 4 6 10])) | ||
(assert-equal 7, (first-repeating-element [6 10 7 4 9 120 4 7])) | ||
(assert-equal 8, (first-repeating-element [8 8 7 4 9 120 4 7]))) |