Skip to content

Commit

Permalink
Merge pull request #137 from MrPhantomT/feature/add_first_repeating_e…
Browse files Browse the repository at this point in the history
…lement_problem

add first repeating element problem
  • Loading branch information
mokevnin committed Oct 7, 2015
2 parents 0d0b37c + c43564f commit d4feeaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions source/first_repeating_element.yml
@@ -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]
16 changes: 16 additions & 0 deletions test/battle_solutions/first_repeating_element_test.clj
@@ -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])))

0 comments on commit d4feeaa

Please sign in to comment.